如何让工具栏覆盖兄弟布局(z 顺序)

How to get Toolbar to overlay sibling layout (z-ordering)

我这辈子都无法将此 Toolbar 布局在其兄弟 LinearLayout 的顶部(注意:不是在上方,而是在其上方)。我可以让 Toolbar 覆盖其兄弟的唯一方法是将 Toolbar 的海拔指定为 > 0,但这不适用于 pre-5.0.

我正在使用父项 FrameLayout,子项 Toolbar 在 XML 中位于其兄弟项 LinearLayout 之后。我错过了什么?即使是 Android Studio 预览窗格也会在 LinearLayout

之上显示 Toolbar

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_main_container"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame"
        android:orientation="vertical"
        >
        <FrameLayout
            android:layout_weight="4"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher"
                />
        </FrameLayout>
        <ListView
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:id="@+id/home_list_listview"
            android:drawSelectorOnTop="true"
            android:divider="@null"
            android:dividerHeight="0dp"
            tools:listitem="@layout/home_list_item"
            >
        </ListView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="@string/slogan"
            android:gravity="center"
            android:textColor="@color/colorPrimaryDark"
            />
    </LinearLayout>

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@android:color/transparent"
        />
</FrameLayout>

原来我有一个片段在主 activity 上提交线性布局,主 activity 已经包含它。糟糕!

那是因为 FrameLayout。 如果您想使用 FrameLayout,视图将不会相互关联。 所以你需要做的就是...

set "height of toolbar" to MarginTop of LinearLayout.

希望,它会有所帮助。