将一个视图置于另一个视图之上

Place a view over another view

我在名为 include_toolbar.xml.

的单独文件中有一个自定义工具栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/include_toolbar"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

在 activity 的布局中,我想在顶部显示此工具栏,在其顶部显示相机预览视图和图像按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/include_toolbar" />

    <com.journeyapps.barcodescanner.CompoundBarcodeView
        app:zxing_result_view="@color/zxing_custom_result_view"
        android:id="@+id/activity_scan_barcodeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.journeyapps.barcodescanner.CompoundBarcodeView>

    <ImageButton
        android:id="@+id/activity_scan_flashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@id/activity_scan_barcodeView"
        android:src="@drawable/btn_flashOn"/>

</LinearLayout>

CompoundBarcodeViewthis 库中的一个组件。

LinearLayout 的问题是图像按钮不显示。

如果我将其更改为 RelativeLayout,则会显示图像按钮,但现在工具栏消失了。

如何让它们同时显示?

将父布局设为 LinearLayout,然后制作两个子组件,一个是工具栏,第二个是 RelativeLayout,在 RelativeLayout 内部放置 CompoundBarcodeViewImageButton.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/include_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:orientation="vertical"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <com.journeyapps.barcodescanner.CompoundBarcodeView
            android:id="@+id/activity_scan_barcodeView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:zxing_result_view="@color/zxing_custom_result_view"/>

        <ImageButton
            android:id="@+id/activity_scan_flashlight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/activity_scan_barcodeView"
            android:layout_alignParentBottom="true"
            android:src="@drawable/btn_flashOn" />

    </RelativeLayout>
</LinearLayout>

您可能需要根据您的设计调整按钮在RelativeLayout中的位置。

<com.journeyapps.barcodescanner.CompoundBarcodeView
    app:zxing_result_view="@color/zxing_custom_result_view"
    android:id="@+id/activity_scan_barcodeView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.journeyapps.barcodescanner.CompoundBarcodeView>

android:layout_height="match_parent" -> android:layout_height="wrap_content"