如何在某些片段中隐藏底部导航栏?

How to hide the bottom navigation bar in certain fragments?

我有一个 activity,带有导航图和带有 2 个菜单项的底部导航栏。 我的问题是My Bottom Navigation Bar无处不在,detailFragment,aboutFragment,signInFragment等等。


        val navController = this.findNavController(R.id.myNavHostFragment)

        val appBarConfiguration = AppBarConfiguration.Builder(
            R.id.contactsFragment,
            R.id.profileFragment
        ).build()

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        NavigationUI.setupWithNavController(navView, navController)


如何限制它只显示在我的菜单项的 2 个片段上?

我就是这样解决的

    navController.addOnDestinationChangedListener{ _, nd: NavDestination, _->
        if(nd.id == R.id.contactsFragment || nd.id == R.id.profileFragment){
            navView.visibility = View.VISIBLE
        }else{
            navView.visibility = View.GONE
        }

对于你的片段,它应该是可见的

navView.visibility = View.VISIBLE

不应该出现的地方

navView.visibility = View.GONE

您是否试图隐藏当前片段的底部导航视图,即在主 activity 或父 activity 中。嗯,就这么简单。只需转到您不需要底部导航视图的片段,然后将以下代码粘贴到 onCreateView 下方。

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val view = requireActivity().findViewById<BottomNavigationView>(R.id.nav_view)

    val fab = requireActivity().findViewById<FloatingActionButton>(R.id.fab)
    view.visibility = View.GONE
    fab.visibility = View.GONE
}