在android中,是否可以将一个视图置于另一个视图之上?
我们能把一个小视图放在另一个大视图上吗?例如,我有一个视频视图,它正在后台播放一个文件。在中间/角落的某个地方,我想放置另一个ImageView在android中,是否可以将一个视图置于另一个视图之上?,android,views,android-videoview,overlapping,Android,Views,Android Videoview,Overlapping,我们能把一个小视图放在另一个大视图上吗?例如,我有一个视频视图,它正在后台播放一个文件。在中间/角落的某个地方,我想放置另一个ImageView 但在线性/相对布局中,视图只能一个接一个或相对放置,建议不要使用绝对布局。那么我该怎么办呢?框架布局允许您将每个视图堆叠在下面的视图之上。这也可以通过RelativeLayout实现,FrameLayout是最简单的视图组,并按照布局XML中定义的顺序(或以编程方式添加)堆叠视图;第一个较低,最后一个位于顶部 下面是一个示例,其中两个视图堆叠并偏移,以
但在线性/相对布局中,视图只能一个接一个或相对放置,建议不要使用绝对布局。那么我该怎么办呢?
框架布局
允许您将每个视图堆叠在下面的视图之上。这也可以通过RelativeLayout
实现,FrameLayout
是最简单的视图组
,并按照布局XML中定义的顺序(或以编程方式添加)堆叠视图
;第一个较低,最后一个位于顶部
下面是一个示例,其中两个视图
堆叠并偏移,以更好地说明这一点:
下面是实际的布局XML,其中有两个重叠的TextView
框。两个框的偏移使用android:layou 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>
您也可以使用谷歌推出的新布局来实现这一点 ConstraintLayout允许使用平面视图层次(无嵌套视图组)创建大型复杂布局
以防万一如果您想在按钮视图顶部放置视图,请使用此按钮;
android:elevation=“7dp”
用于需要放置在按钮顶部的视图。RelativeLayout可以工作,但您可以使用FrameLayout。您能给我一个这两种情况的示例吗?我想不出来。@kiki-Google“android FrameLayout”,你会发现很多例子。使用FrameLayout,因为它允许你在视图之间重叠。它起作用了,但对我来说1dp up也起作用了。谢谢,我花了好几个小时才发回按钮。你救了我一天,这正是我所需要的。这个答案是纯金的!
<?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/yellow"
>
<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>
<?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>