简体   繁体   English

如何从底部应用程序栏布局隐藏浮动操作按钮

[英]How to hide the Floating Action Button from Bottom App Bar Layout

I'm creating a Bottom App Bar with an Floating Action Button.我正在创建一个带有浮动操作按钮的 底部应用栏 The App Bar is part of my Main Activity and I want to hide the FAB for some Fragments within the Activity.应用程序栏是我的主要活动的一部分,我想隐藏活动中某些片段的 FAB。

I already tried 'View.GONE' and 'fab.hide()'.我已经尝试过“View.GONE”和“fab.hide()”。 Then I tried to hide the Fab with following function:然后我尝试使用以下功能隐藏 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()
    }

My layout.xml:我的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>

Just tested out with fab.hide() method and it works.刚刚使用 fab.hide() 方法进行了测试,它可以工作。 The "logic" to hide the fab should occur in the activity and not in the fragment.隐藏 fab 的“逻辑”应该出现在活动中而不是片段中。 The next logic is set in the activity and the hide part is inside the else branch at the end.下一个逻辑在活动中设置,隐藏部分在最后的 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()
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM