我使用导航图进行导航,并将启动屏幕(splash fragment(作为显示的第一个片段。问题是,当我转到主片段/屏幕时,顶部导航按钮会显示回来,而不是抽屉图标。我们如何控制这件事?有哪些选择。如何更改起始目的地?(如果可能(
当用户位于顶级目的地时,"导航"按钮如果目的地使用抽屉布局,则变为抽屉图标。如果目的地不使用抽屉布局,导航按钮为隐藏的当用户在任何其他目的地时,导航按钮显示为"向上"按钮配置"导航"按钮仅使用起始目的地作为顶级目的地,创建AppBarConfiguration对象,并传入相应的导航图,如下所示:
示例代码
我的问题是,当导航到next_frage时,返回按钮仍然存在。它应该显示菜单/汉堡图标。
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration : AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
val navController = this.findNavController(R.id.mainNavigationHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout)
NavigationUI.setupWithNavController(binding.mainNavigationDrawerView, navController)
val navigationHeader = binding.mainNavigationDrawerView.getHeaderView(0)
val iconButton = navigationHeader.findViewById<ImageButton>(R.id.main_nav_icon_button)
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.mainNavigationHostFragment)
return NavigationUI.navigateUp(navController, appBarConfiguration)
}
}
不是将Navigation Graph
传递给AppbarConfiguration
,而是传递应该显示HamBurger icon
的片段的id,这样,当显示特定的片段时,它会显示主页/汉堡图标,而不是返回的图标。
这就是
val appBarConfiguration = AppBarConfiguration(setOf(R.id.FAGMENT_THAT_SHOULD_SHOW_HOME_ICON), DRAWER_LAYOUT)
更新
而不是
val navController = this.findNavController(R.id.mainNavigationHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout)
NavigationUI.setupWithNavController(binding.mainNavigationDrawerView, navController)
val navigationHeader = binding.mainNavigationDrawerView.getHeaderView(0)
val iconButton = navigationHeader.findViewById<ImageButton>(R.id.main_nav_icon_button)
只是
val navController = this.findNavController(R.id.mainNavigationHostFragment)
appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout)
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);