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

时间:2019-05-20 07:25:22

标签: android android-layout kotlin

我正在使用浮动操作按钮创建一个Bottom App Bar。应用栏是我主要活动的一部分,我想隐藏活动中某些片段的FAB。

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

/u01/ABC/a/1.txt
/u01/ABC/b/1.txt
/u01/ABC/c/1.txt

我的layout.xml:

    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()
    }

2 个答案:

答案 0 :(得分:0)

只需使用fab.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()
        }

    }

答案 1 :(得分:0)

由于layout_anchor属性而发生

CoordinatorLayout.LayoutParams layout= (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
layout.setAnchorId(View.NO_ID);
fab.setLayoutParams(p);
fab.setVisibility(View.GONE);