【问题标题】:How to hide the Floating Action Button from Bottom App Bar Layout如何从底部应用栏布局中隐藏浮动操作按钮
【发布时间】:2019-05-20 07:25:22
【问题描述】:

我正在创建一个带有浮动操作按钮的Bottom App Bar。 App Bar 是我的 Main Activity 的一部分,我想隐藏 Activity 中某些 Fragments 的 FAB。

我已经尝试过“View.GONE”和“fab.hide()”。然后我尝试使用以下功能隐藏 Fab:

    private fun hideFloatingActionButton() {
        val params = fab.layoutParams as CoordinatorLayout.LayoutParams
        val behavior = params.behavior as FloatingActionButton.Behavior?

        if (behavior != null) {
            behavior.isAutoHideEnabled = false
        }

        params.anchorId = View.NO_ID;
        params.width = 0;
        params.height = 0;
        fab.layoutParams = params
        fab.hide()
    }

我的 layout.xml:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        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"
        tools:context=".MainActivity"
        android:background="@color/backPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    ...

         content
    ...

    <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            app:navigationIcon="@drawable/ic_burger_menu"
            app:fabAlignmentMode="end"
    />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/start_project_day"
            app:srcCompat="@drawable/ic_next"
            app:layout_anchor="@id/bar"
    />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

  • 什么时候调用hideFloatingActionButton 方法
  • 我正在使用导航组件。当片段启动时,我会向 Activity 发送一个事件,并在必要时从 Activity 调用“hideFloatingActionButton()”。

标签: android android-layout kotlin


【解决方案1】:

刚刚使用 fab.hide() 方法进行了测试,它可以工作。隐藏工厂的“逻辑”应该发生在活动中,而不是片段中。下一个逻辑在activity中设置,hide部分在最后的else分支里面。

navController.addOnDestinationChangedListener { controller, destination, _ ->

        bar.animate().translationY(0f)


        // First page is main menu
        if(controller.graph.startDestination == destination.id){


            bar.navigationIcon = icon
            bar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
            fab?.setImageDrawable(getDrawable(R.drawable.ic_local_wtf))

        }else{

            // Hide navigation drawer icon
            bar.navigationIcon = null

            // Move FAB from the center of BottomAppBar to the end of it
            bar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END



            // Replace the action menu
            //bar.replaceMenu(bottomappbar_menu_secondary)
            invalidateOptionsMenu()

            // Change FAB icon
            fab?.setImageDrawable(getDrawable(R.drawable.ic_reply_white_24dp))
            fab.hide()
        }

    }

【讨论】:

  • 我也在使用导航组件。当片段启动时,我会向活动发送一个事件,并在必要时从活动中调用“hideFloatingActionButton()”。问题是,我有一个嵌套的导航图,因此我无法使用OnDestinationChangedListener
  • 我认为您的活动有问题。我刚刚做了一个愚蠢的测试 (requireActivity() as HomeActivity/*cast to your activity*/).hideTheFAB() 在我的 Fragment 里面 onActivityCreated 方法和我的 Activity hideTheFab 方法里面我只添加 fab.hide() 它仍然有效往这边走
  • 我在我的事件系统中发现了一个问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2016-07-27
  • 1970-01-01
  • 2022-10-05
  • 2021-01-27
相关资源
最近更新 更多