1

活动布局有点复杂...是一个片段活动,底部片段('bottomBarFragmentPlaceholder')是底部AppBar,FAB放在中间。

活动布局如下:

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

4

1 回答 1

0

好吧,玩弄了不同的布局,直到我终于完成了一切……

重要见解:

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

  • bottomAppBar 布局不能使用 layout_height="wrap_content",它需要能够占用父级的所有空间......(“match_parent”),这就是 FAB 将被赋予足够的高度以正确放置在摇篮。

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

Fragment Activity 布局如下:

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

于 2019-01-04T15:02:47.867 回答