2010-09-29 57 views
11

Chúng tôi có thể đặt chế độ xem nhỏ hơn một chế độ xem lớn khác không? Ví dụ, tôi có một VideoView đang phát một tệp ở chế độ nền. Trên đây, một nơi nào đó ở giữa/góc, tôi muốn đặt một ImageView khác.Có thể đặt một chế độ xem trên một chế độ xem khác trong Android không?

Nhưng trong bố cục tuyến tính/tương đối, lượt xem có thể được đặt chỉ cái khác hoặc tương đối với nhau. Và Absolutelayout được khuyên chống lại. Vậy tôi phải làm gì?

+1

RelativeLayout sẽ hoạt động nhưng bạn có thể sử dụng FrameLayout. – bhups

Trả lời

11

FrameLayouts cho phép bạn nén từng chế độ xem trên đầu trang bên dưới. Điều này cũng có thể đạt được với một số RelativeLayout.

+1

Bạn có thể cho tôi một ví dụ về cả hai trường hợp không? Tôi không thể nghĩ ra được. – kiki

+1

@kiki - Google "android FrameLayout" và bạn sẽ tìm thấy nhiều ví dụ. – McStretch

+2

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

8

FrameLayout là đơn giản nhất ViewGroup và ngăn xếp Views theo thứ tự chúng được xác định trong XML bố cục; cái đầu tiên sẽ thấp hơn, và cái cuối cùng sẽ ở trên đầu.

Dưới đây là một ví dụ nơi View s được bù đắp để minh họa rõ hơn về điểm:

enter image description here

Đây là XML bố trí với hai chồng chéo TextView hộp. Độ lệch của hai hộp được thực hiện bằng cách sử dụng android:layout_gravity trong khi android:gravity là để căn giữa chính văn bản trong mỗi hộp.

<?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

Bạn cũng có thể làm điều đó bằng cách sử dụng ConstraintLayout một bố cục mới được giới thiệu bởi google.

ConstraintLayout cho phép bạn tạo bố cục lớn và phức tạp với phân cấp chế độ xem phẳng (không có nhóm xem lồng nhau).

<?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> 
Các vấn đề liên quan