我有一个固定在底部栏上的 FAB,它有一个错误的行为

I have a FAB anchored to a bottom bar that have a bugged behavior

我正在开发一个 Android 应用程序,其中有一个 Activity,有一个 bottomBar 和一个 Floating Action Button 锚定。 我的做法是这样的:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabNewIssue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/strong_blue"
        android:contentDescription="Add Issue"
        app:srcCompat="@drawable/ic_outline_add_24"
        app:tint="@color/white"
        app:fabCustomSize="60dp"
        app:layout_anchor="@+id/bottomBar"
        app:layout_anchorGravity="top|center"/>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        app:fabAlignmentMode="center"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:menu="@menu/bottom_nav_menu"/>

</com.google.android.material.bottomappbar.BottomAppBar>

接下来我要做的是,在某些片段中,我需要隐藏BottomBarFAB,我的做法是:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        (activity as MainActivity).let {
            it.toolbarText = "Olá " + it.user?.name
            it.appbar.setExpanded(true)
            it.binding.fabNewIssue.visibility = View.GONE
            it.binding.bottomBar.visibility = View.GONE
            it.binding.mainFragmentContainer.setPadding(0,0,0, 
                      Utils().convertDpToPixel(60f,requireContext()).toInt())
        }
    }

我对此行为没有问题,当我尝试返回并再次显示 BottomBarFAB 时,我的问题就来了。出现bug,圆条效果消失如图:

First Fragment with normal Bar

Second Fragment with hidden Bar

First Fragment with bugged Bar

有人知道怎么解决吗?

尝试将底部栏和 FAB 嵌套在容器中,然后 hide/show 使用该布局。

XML布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/bottomBarContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fabNewIssue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/strong_blue"
            android:contentDescription="Add Issue"
            app:srcCompat="@drawable/ic_outline_add_24"
            app:tint="@color/white"
            app:fabCustomSize="60dp"
            app:layout_anchor="@+id/bottomBar"
            app:layout_anchorGravity="top|center"/>

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            app:fabAlignmentMode="center"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="10dp">

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginEnd="16dp"
                android:background="@android:color/transparent"
                app:menu="@menu/bottom_nav_menu"/>

    </com.google.android.material.bottomappbar.BottomAppBar>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Activity代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    (activity as MainActivity).let {
        it.toolbarText = "Olá " + it.user?.name
        it.appbar.setExpanded(true)
        it.binding.bottomBarContainer.visibility = View.GONE
        it.binding.mainFragmentContainer.setPadding(0,0,0, 
                  Utils().convertDpToPixel(60f,requireContext()).toInt())
    }
}