不使用开始作为顶级导航时,如何配置抽屉式导航按钮?
How to configure the Drawer Navigation button when not using the start as the top-level navigation?
我正在使用导航图进行导航,并将开始屏幕(启动画面片段)作为显示的第一个片段。问题是,当我转到主 fragment/screen 时,顶部导航按钮显示回来而不是抽屉图标。
我们怎样才能控制这个?有哪些选择。如何更改起始目的地? (如果可能的话)
When the user is at a top-level destination, the Navigation button
becomes a drawer icon if the destination uses a DrawerLayout. If the
destination doesn't use a DrawerLayout, the Navigation button is
hidden. When the user is on any other destination, the Navigation
button appears as an Up button . To configure the Navigation button
using only the start destination as the top-level destination, create
an AppBarConfiguration object, and pass in the corresponding
navigation graph, as shown below:
示例代码
我的问题是导航到 next_fragment 时后退按钮仍然存在。它应该显示 menu/hamburger 图标。
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,这样当显示了特定的 Fragment,它显示 home/hamburger 图标而不是 back 图标。
也就是
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);
我正在使用导航图进行导航,并将开始屏幕(启动画面片段)作为显示的第一个片段。问题是,当我转到主 fragment/screen 时,顶部导航按钮显示回来而不是抽屉图标。 我们怎样才能控制这个?有哪些选择。如何更改起始目的地? (如果可能的话)
When the user is at a top-level destination, the Navigation button becomes a drawer icon if the destination uses a DrawerLayout. If the destination doesn't use a DrawerLayout, the Navigation button is hidden. When the user is on any other destination, the Navigation button appears as an Up button . To configure the Navigation button using only the start destination as the top-level destination, create an AppBarConfiguration object, and pass in the corresponding navigation graph, as shown below:
示例代码
我的问题是导航到 next_fragment 时后退按钮仍然存在。它应该显示 menu/hamburger 图标。
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,这样当显示了特定的 Fragment,它显示 home/hamburger 图标而不是 back 图标。
也就是
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);