将小部件放在彼此之上

Place widgets on top of each other

我有一个相机小部件 "background",我想在它上面有一个图像和一个按钮。我应该使用什么布局来将图像和按钮正确放置在相机小部件的顶部?

以下布局将相机小部件推出屏幕(只有图像和按钮可见):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <eu.livotov.labs.android.camview.CAMView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/zxScanCamera"/>

    <ImageView
        android:src="@drawable/hud"
        android:scaleType="centerInside"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        style="@style/StdText"
        android:id="@+id/flashLight"
        android:layout_width="match_parent"
        android:text="@string/scan_flash_on"
        android:background="?android:attr/selectableItemBackground"/>

</FrameLayout>

如果您想对齐任何子视图,例如按钮应该在顶部和中间,那么最好选择 RelativeLayout

使用 RelativeLayout - 它使用其子项的定义顺序作为它们的 Z 顺序(在彼此之上)。首先声明您的 CAMView 然后(作为资源文件中的兄弟姐妹)- 按钮和图像视图。当然,您应该考虑它们在屏幕上的相对位置(在它们的父级内部),但简而言之 - 如果下一个子级在 x,y 坐标中重叠,则会绘制前一个子级的 "on top"。

你的情况:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <eu.livotov.labs.android.camview.CAMView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        .../>

    <ImageView
        ...
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

    <Button
        ...
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

使用 android:layout_alignParent... 属性将您的项目放置在其父项目中

如果您使用约束布局,则列表下方的小部件将显示在应用程序中所有其他小部件的上方。See this image例如,imageView1 将显示在 imageView2 的顶部。 (从上面或顶部,我的意思是朝向用户或朝向屏幕外。imageView1 将出现在 imageView2 上方,就像您将一张纸放在一张大纸上一样)