如何将FAB置于布局的顶部

时间:2018-09-26 13:52:32

标签: android android-layout floating-action-button

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

我如何将它们带到最前面?

enter image description here

<?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>

2 个答案:

答案 0 :(得分:1)

LinearLayout不允许子视图重叠。

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

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

答案 1 :(得分:1)

要平均分布晶圆厂并将其用作网络视图的覆盖层,请看一下我的代码:

enter image description here

<?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>