是否有可能在android中放置一个视图而不是另一个视图?
问题描述:
我们可以在另一个大视图上放一个小视图吗?例如,我有一个VideoView在后台播放文件。在此,在中间/角落的某处,我想放置另一个ImageView。是否有可能在android中放置一个视图而不是另一个视图?
但是在线性/相对布局中,视图可以一个接一个放置或彼此相对。并建议Absolutelayout。那么我该怎么做?
答
FrameLayouts
让你把每个视图堆在下面的视图上。这也可以通过RelativeLayout
来实现。
答
FrameLayout
是最简单的ViewGroup
并按照它们在布局XML中定义的顺序堆叠Views
;第一个将会更低,而最后一个将会排在最前面。
下面是一个例子,其中View
s的偏移,以更好地说明这一点:
下面是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>
RelativeLayout将工作,但您可以使用FrameLayout。 – bhups 2010-09-29 12:52:20