FAB 不浮动

FAB isn't floating

activity 布局有点复杂...它是一个片段 activity,底部片段 ('bottomBarFragmentPlaceholder') 是放置 FAB 的 bottomAppBar。

activity布局如下:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/progressBarTimeText"
                android:layout_alignBottom="@id/progressBarTimeText"
                android:scaleY="6" />

            <TextView
                android:id="@+id/progressBarTimeText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="#00000000"
                android:gravity="center" />
        </RelativeLayout>

        <androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <FrameLayout
            android:id="@+id/bottomBarFragmentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</FrameLayout>

那么BottomAppBar布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/primaryLightColor"
        android:backgroundTint="@color/primaryLightColor"
        app:fabAlignmentMode="center"
        app:menu="@drawable/ic_action_overflow"
        app:navigationIcon="@drawable/ic_menu_24"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
        app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/baseline_publish_black_24dp"
            app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

为什么我的 FAB 被推下而不是放入支架中?

非常感谢任何帮助。

谢谢, GBa.

嗯,试过不同的布局,直到我终于把它全部设置好...

主要见解:

  • 上层不需要允许覆盖功能(浮动按钮)所需的 FrameLayout,但仅在尽可能低的层中需要...(请参阅下面更新的布局)

  • bottomAppBar布局不能和layout_height="wrap_content",需要赋予父级全部space的能力...( "match_parent"),这就是 FAB 获得足够高度以正确放置在支架中的方式。

最后,这是更新后的布局:

FragmentActivity布局如下:

<?xml version="1.0" encoding="utf-8"?><!-- practice_activity.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/progressBarTimeText"
            android:layout_alignBottom="@id/progressBarTimeText"
            android:scaleY="6" />

        <TextView
            android:id="@+id/progressBarTimeText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="#00000000"
            android:gravity="center" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_gravity="center|fill_vertical"
            android:layout_height="match_parent" />

        <FrameLayout
            android:id="@+id/bottomBarFragmentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"/>
    </FrameLayout>
</LinearLayout>

BottomAppBar布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/primaryLightColor"
        android:backgroundTint="@color/primaryLightColor"
        app:fabAlignmentMode="center"
        app:menu="@drawable/ic_action_overflow"
        app:navigationIcon="@drawable/ic_menu_24"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
        app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/baseline_publish_black_24dp"
            app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我希望这对将来会遇到类似问题的其他人有所帮助...

编码愉快... GBa.