是否有可能发生在另外一个视图中的Android?
-
26-09-2019 - |
题
我们可以把对另一大视野小的看法?例如,我有它在后台播放文件的VideoView。在此,在中间某个地方/角落,我希望把另一个ImageView的。
但是,在线性/相对布局,视图可以仅放置一个接一个或相对于彼此,并AbsoluteLayout被防止建议。所以,我该怎么办?
解决方案
FrameLayouts
让你桩上下面的一个上的每个视图。这也可以用一个RelativeLayout
实现。
其他提示
在FrameLayout
是最简单的ViewGroup
并堆叠在他们在布局定义XML(或编程方式添加)的顺序Views
;第一会更低,最后将在上面。
下面是两个Views
被堆叠和偏移,以更好地示例说明这一点:
下面是实际的布局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>
就在,如果你要放置一个视图中的ButtonView的顶部,然后利用这种情况下;
android:elevation="7dp"
为需要被放置在按钮的顶部视图。
您还可以通过使用 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>
<?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>
不隶属于 StackOverflow