单击抽屉后如何继续显示汉堡包图标而不是 back/up 图标?
How to keep showing the hamburger icon instead of the back/up icon after clicked on the drawer?
我正在关注代码实验室 https://developer.android.com/codelabs/kotlin-android-training-add-navigation/index.html#9,我使用以下代码连接抽屉:
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout : DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
val navController = findNavController(R.id.myNavHostFragment)
// To support up action and also the title, the third parameter is optional and is used
// for the hamburger menu for drawer.
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
// show the navigation drawer
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.myNavHostFragment)
// this is for the hamburger menu
return NavigationUI.navigateUp(navController, drawerLayout)
}
}
显示初始片段时,汉堡包按预期显示:
但是,在我从抽屉中选择了一件物品之后,例如左右,变成了back/up按钮:
如何继续使用汉堡包按钮而不是将其更改为向上按钮?
代码实验室的完整代码在这里:https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/AndroidTriviaNavigation
我认为如果您在 navdrawer_menu.xml 文件中添加布局,汉堡包图标将保留
根据 AppBarConfiguration documentation:
NavigationUI
uses an AppBarConfiguratio
n object to manage the behavior of the Navigation button in the upper-left corner of your app's display area. The Navigation button’s behavior changes depending on whether the user is at a top-level destination.
A top-level destination is the root, or highest level destination, in a set of hierarchically-related destinations. Top-level destinations do not display an Up button in the top app bar because there is no higher level destination. By default, the start destination of your app is the only top-level destination.
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.
因此,如果您想让抽屉图标出现在多个目的地而不仅仅是起始目的地,您需要创建一个 AppBarConfiguration
对象,其中包含您希望成为顶级目的地的确切目的地:
// Use the IDs from your navigation graph
val appBarConfiguration = AppBarConfiguration(
setOf(R.id.rulesFragment, R.id.aboutFragment),
drawerLayout)
因此,您的代码变为:
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration : AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
// Use the Kotlin extension in navigation-ui-ktx to create
// the AppBarConfiguration
appBarConfiguration = AppBarConfiguration(
setOf(R.id.rulesFragment, R.id.aboutFragment),
binding.drawerlayout)
val navController = findNavController(R.id.myNavHostFragment)
// To support up action and also the title, the third parameter is optional and is used
// for the hamburger menu for drawer.
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
// show the navigation drawer
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.myNavHostFragment)
// this is for the hamburger menu
return NavigationUI.navigateUp(navController, appBarConfiguration)
}
}
我正在关注代码实验室 https://developer.android.com/codelabs/kotlin-android-training-add-navigation/index.html#9,我使用以下代码连接抽屉:
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout : DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
val navController = findNavController(R.id.myNavHostFragment)
// To support up action and also the title, the third parameter is optional and is used
// for the hamburger menu for drawer.
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
// show the navigation drawer
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.myNavHostFragment)
// this is for the hamburger menu
return NavigationUI.navigateUp(navController, drawerLayout)
}
}
显示初始片段时,汉堡包按预期显示:
但是,在我从抽屉中选择了一件物品之后,例如左右,变成了back/up按钮:
如何继续使用汉堡包按钮而不是将其更改为向上按钮?
代码实验室的完整代码在这里:https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/AndroidTriviaNavigation
我认为如果您在 navdrawer_menu.xml 文件中添加布局,汉堡包图标将保留
根据 AppBarConfiguration documentation:
NavigationUI
uses anAppBarConfiguratio
n object to manage the behavior of the Navigation button in the upper-left corner of your app's display area. The Navigation button’s behavior changes depending on whether the user is at a top-level destination.A top-level destination is the root, or highest level destination, in a set of hierarchically-related destinations. Top-level destinations do not display an Up button in the top app bar because there is no higher level destination. By default, the start destination of your app is the only top-level destination.
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.
因此,如果您想让抽屉图标出现在多个目的地而不仅仅是起始目的地,您需要创建一个 AppBarConfiguration
对象,其中包含您希望成为顶级目的地的确切目的地:
// Use the IDs from your navigation graph
val appBarConfiguration = AppBarConfiguration(
setOf(R.id.rulesFragment, R.id.aboutFragment),
drawerLayout)
因此,您的代码变为:
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration : AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
// Use the Kotlin extension in navigation-ui-ktx to create
// the AppBarConfiguration
appBarConfiguration = AppBarConfiguration(
setOf(R.id.rulesFragment, R.id.aboutFragment),
binding.drawerlayout)
val navController = findNavController(R.id.myNavHostFragment)
// To support up action and also the title, the third parameter is optional and is used
// for the hamburger menu for drawer.
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
// show the navigation drawer
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.myNavHostFragment)
// this is for the hamburger menu
return NavigationUI.navigateUp(navController, appBarConfiguration)
}
}