我有一个带有导航图的活动和一个带有 2 个菜单项的底部导航栏。我的问题是我的底部导航栏出现在任何地方,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
您是否正在尝试从当前片段(即主活动或父活动(中隐藏底部导航视图。嗯,就是这么简单。只需转到您不需要底部导航视图的片段,然后在 CreateView 上粘贴以下代码。
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
}