gpt4 book ai didi

android - 单击抽屉后如何继续显示汉堡包图标而不是后退/向上图标?

转载 作者:行者123 更新时间:2023-12-05 00:15:37 25 4
gpt4 key购买 nike

我正在关注代码实验室 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)
}
}

显示初始 fragment 时,汉堡包按预期显示:

initial fragment

但是,在我从抽屉中选择了一项后,例如关于,它变成了一个后退/上一步按钮:

enter image description here

如何继续使用汉堡按钮而不是将其更改为向上按钮?

codelab 的完整代码在这里:https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/AndroidTriviaNavigation

最佳答案

根据 AppBarConfiguration documentation :

NavigationUI uses an AppBarConfiguration 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.

因此,如果您想让 Drawer 图标出现在多个目的地而不仅仅是起始目的地,您需要创建一个 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)
}
}

关于android - 单击抽屉后如何继续显示汉堡包图标而不是后退/向上图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65877046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com