是否有可能在android中放置一个视图而不是另一个视图?

问题描述:

我们可以在另一个大视图上放一个小视图吗?例如,我有一个VideoView在后台播放文件。在此,在中间/角落的某处,我想放置另一个ImageView。是否有可能在android中放置一个视图而不是另一个视图?

但是在线性/相对布局中,视图可以一个接一个放置或彼此相对。并建议Absolutelayout。那么我该怎么做?

+1

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

FrameLayouts让你把每个视图堆在下面的视图上。这也可以通过RelativeLayout来实现。

+1

你能举个例子吗?我无法想出来。 – kiki 2010-09-29 12:54:11

+1

@kiki - Google“android FrameLayout”,你会发现很多例子。 – McStretch 2010-09-29 13:47:34

+2

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

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> 

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

您也可以通过使用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>