2010-09-29 25 views
11

我們可以在另一個大視圖上放一個小視圖嗎?例如,我有一個VideoView在後臺播放文件。在此,在中間/角落的某處,我想放置另一個ImageView。是否有可能在android中放置一個視圖而不是另一個視圖?

但是在線性/相對佈局中,視圖可以一個接一個放置或彼此相對。並建議Absolutelayout。那麼我該怎麼做?

+1

RelativeLayout將工作,但您可以使用FrameLayout。 – bhups 2010-09-29 12:52:20

回答

11

FrameLayouts讓你把每個視圖堆在下面的視圖上。這也可以通過RelativeLayout來實現。

+1

你能舉個例子嗎?我無法想出來。 – kiki 2010-09-29 12:54:11

+1

@kiki - Google「android FrameLayout」,你會發現很多例子。 – McStretch 2010-09-29 13:47:34

+2

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

8

FrameLayout是最簡單的ViewGroup並按照它們在佈局XML中定義的順序堆疊Views;第一個將會更低,而最後一個將會排在最前面。

下面是一個例子,其中View s的偏移,以更好地說明這一點:

enter image description here

下面是XML佈局與兩個重疊TextView框。兩個框的偏移量使用android:layout_gravity完成,而android:gravity用於將文本本身置於每個框內。

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

您也可以通過使用ConstraintLayout由谷歌推出了一個新的佈局做到這一點。

ConstraintLayout允許您創建具有平面視圖層次結構(無嵌套視圖組)的大而複雜的佈局。

<?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> 
相關問題