如何将 FAB 置于布局顶部

How to bring the FAB to the top of the layout

我正在尝试在两个视图之间设置 3 个 FAB。但是 FAB 位于顶视图下方,如下所示。

如何将它们带到前面?

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/parentPanel"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            card_view:cardCornerRadius="6dp"
            android:layout_margin="10dp"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?android:selectableItemBackground"
            android:id="@+id/deviceCard">

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="io.tegaru.beepr.AddNewMapsLocationActivity" />

        </android.support.v7.widget.CardView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="4"
        android:background="@android:color/white">

        <LinearLayout
            android:id="@+id/btnPanel1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/btnPanel2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/btnPanel3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

LinearLayout 不允许子视图重叠。

如果您想在其他视图上方(重叠)显示 FAB,您应该使用 RelativeLayout(或 FrameLayout 等)而不是 LinearLayout 作为父视图。

在您的情况下,将根布局设置为 RelativeLayout。不过,您可能需要进行其他调整。

对于 space 晶圆厂,例如,将它们用作网络视图的叠加层,请查看我的代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <WebView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></WebView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fab2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fab3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/fab1" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/fab2" />

</android.support.constraint.ConstraintLayout>