2010-09-29 12 views
11

Czy możemy umieścić mały widok na inny duży widok? Na przykład mam VideoView, który odtwarza plik w tle. Ponad to, gdzieś w środku/rogu, chcę umieścić inny ImageView.Czy możliwe jest umieszczenie jednego widoku na innym w systemie Android?

Jednak w układzie liniowym/względnym widoki można umieszczać tylko jeden po drugim lub porównywać się nawzajem. I Absolutelayout jest odradzany. Więc co mam zrobić?

+1

RelativeLayout będzie działać, ale można użyć FrameLayout. – bhups

Odpowiedz

11

FrameLayouts pozwala umieścić każdy widok na jeden poniżej. Można to również osiągnąć dzięki RelativeLayout.

+1

Czy możesz podać mi przykład obu przypadków? Nie jestem w stanie tego wymyślić. – kiki

+1

@kiki - Google "android FrameLayout", a znajdziesz mnóstwo przykładów. – McStretch

+2

http://stackoverflow.com/questions/6690530/how-to-show-one-layout-on-the-other-programmically-in-case-case – nograde

8

Najprostszy jest kod ViewGroup i jest układany w kolejności, w jakiej są zdefiniowane w układzie XML; pierwszy będzie niższy, a ostatni będzie na górze.

Oto przykład, gdzie View s są przesunięte w celu lepszego zilustrowania punkt:

enter image description here

Oto XML układ z dwóch nakładających TextView polach. Przesunięcie dwóch pól odbywa się za pomocą android:layout_gravity, podczas gdy android:gravity służy do centrowania samego tekstu w każdym polu.

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="100dp" 
    android:layout_height="100dp"> 

    <TextView 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:layout_gravity="top|left" 
     android:background="@android:color/holo_blue_light" 
     android:gravity="center" 
     android:text="First is below"/> 

    <TextView 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:layout_gravity="bottom|right" 
     android:background="@android:color/holo_green_light" 
     android:gravity="center" 
     android:text=" Last is on top"/> 

</FrameLayout> 
1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/dp_20" 
    android:background="@color/yellowt" 
    > 
     <VideoView 
      android:id="@+id/videoview" 
      android:layout_width="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_height="300dp" 
      android:contentDescription="@string/app_name" 
      /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/camera" 
      android:layout_margin="30dp" 
      android:layout_alignParentEnd="true" 
      android:layout_centerVertical="true" 
      android:id="@+id/imageView" /> 

</RelativeLayout> 
0

Można również zrobić to za pomocą ConstraintLayout nowy układ wprowadzony przez google.

Funkcja ConstraintLayout umożliwia tworzenie dużych i złożonych układów z płaską hierarchią widoku (bez zagnieżdżonych grup widoków).

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="4" 
    tools:context="com.edalat.example.MainActivity"> 
    <VideoView 
    android:id="@+id/videoView" 
    android:layout_width="283dp" 
    android:layout_height="349dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginBottom="24dp" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="24dp" 
    android:layout_marginRight="24dp" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginLeft="24dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintHorizontal_bias="0.509"/> 
    <ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@mipmap/ic_launcher" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="24dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginBottom="24dp" 
    android:layout_marginLeft="24dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    android:layout_marginRight="24dp" 
    app:layout_constraintRight_toRightOf="parent"/> 
    </android.support.constraint.ConstraintLayout> 
Powiązane problemy