FrameLayout 内的 FAB 位于片段之上

FAB inside FrameLayout to be on top of fragments

在我的布局中,我有一个 FrameLayout,我打算在其中根据用户输入显示不同的布局。我想在所有这些布局中显示 FloatingActionButton

为了不重复所有的FloatingActionButton,我想把它附加到FrameLayout。问题是按钮没有正确显示在 fragment 布局的顶部。例如,最初显示的fragment,有一个GridView,按钮在它后面。

activity_main.xml:

<?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"
    android:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="4">

        <LinearLayout
            android:id="@+id/categories_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingRight="20dp">

            <ExpandableListView
                android:id="@+id/categories_listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/mainPane"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/settings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|start"
                android:layout_margin="8dp"
                android:clickable="true"
                android:elevation="5dp"
                android:src="@android:drawable/ic_menu_search" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>

fragment 布局以编程方式显示:

<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:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipeRefreshContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="@dimen/activity_vertical_margin">

            <GridView
                android:id="@+id/productsGridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:numColumns="3"></GridView>

        </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

结果:

改成这样,FAB和fragment不共享同一个FrameLayout:

<?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"
    android:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="4">

        <LinearLayout
            android:id="@+id/categories_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingRight="20dp">

            <ExpandableListView
                android:id="@+id/categories_listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/mainPane"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"/>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="8dp"
        android:clickable="true"
        android:elevation="5dp"
        android:src="@android:drawable/ic_menu_search" />

    </FrameLayout>
</LinearLayout>

FrameLayout 中声明另一个 Framelayout 并在那里扩充片段。它应该有效。

... <LinearLayout>
     .....

   <FrameLayout
        android:id="@+id/mainPane"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <FrameLayout
            android:id="@+id/fragment_inflate"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="8dp"
            android:clickable="true"
            android:elevation="5dp"
            android:src="@android:drawable/ic_menu_search" />
    </FrameLayout>

在不使用坐标布局的情况下在框架布局中添加 FAB

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/framelayout"
        android:layout_below="@+id/drawer_toolbar"
        android:layout_above="@+id/drawer_foot" >
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/shareFab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="8dp"
            android:clickable="true"
            android:elevation="5dp"
            android:src="@android:drawable/ic_menu_share" />
    </FrameLayout>

找到您的框架布局并调用此方法并在此方法中传递您的框架布局 100% 它会起作用

    private void addFloatingButton(FrameLayout frameLayout) {
    int i20 = 20;
    int i50 = 50;
    FrameLayout.LayoutParams clFloatingButtonParams = new FrameLayout.LayoutParams(i50, i50);
    clFloatingButtonParams.setMargins(0, 0, i20, i20);
    clFloatingButtonParams.gravity = Gravity.BOTTOM | Gravity.END;
    FloatingActionButton clFloatingActionButton = new FloatingActionButton(getActivity());
    clFloatingActionButton.setId(R.id.calander_view_fab);
                 frameLayout.addView(clFloatingActionButton, clFloatingButtonParams);

      }

你可以把layout_gravity添加到FAB,然后你就可以把它放在你想要的任何角落了。

右下角设置:

android:layout_gravity="right|bottom"

左下角设置:

android:layout_gravity="left|bottom"

左上角设置:

android:layout_gravity="left|top"

右上角设置:

android:layout_gravity="right|top"