- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在关注代码实验室 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 时,汉堡包按预期显示:
但是,在我从抽屉中选择了一项后,例如关于,它变成了一个后退/上一步按钮:
如何继续使用汉堡按钮而不是将其更改为向上按钮?
codelab 的完整代码在这里:https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/AndroidTriviaNavigation
最佳答案
根据 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.
因此,如果您想让 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/
我需要将抽屉导航图标(汉堡包)从操作栏移动到选项卡布局,以使其看起来像提供的示例。那应该在列表滚动上执行。是否有可能将 View 从操作栏移动到选项卡布局? 操作栏 预期结果 最佳答案 您可以像这样在
我一直在寻找这个问题很长一段时间,但找不到任何解决方案。 我想实现这种按钮,当抽屉关闭时,它看起来像一个汉堡包抽屉导航按钮。但是当它打开时,图标会动画并转换为“后退按钮”,如图所示。 最佳答案 那是
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-top
我对 html、css、js 和 bootstrap 4 还很陌生。我正在尝试建立一个网站来练习我的技能。我终于设法改变了我的 bootstrap 4 导航栏的颜色、文本、字体大小等,但是当我缩小时,
我想为我的导航栏设置自定义颜色 (#5f788a),但是,据我所知,为了在移动版本中使用切换菜单,导航栏类必须是 navbar-light或 navbar-dark(根据 Bootstrap)。当然,
只是想知道是否有任何方法可以调整 Bootstrap 的导航栏切换器图标的大小? 我使用 .navbar-dark 主题作为模板,尺寸如下: .navbar-dark { background-c
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this q
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 2年前关闭。 Improve t
我正在使用 bootstrap 4 构建这个导航栏。 导航栏工作正常,但有一个问题我无法自己解决。 在折叠时,汉堡包图标显示两行而不是三行。我不确定汉堡包为什么会这样出现,但我确定它与伪元素有关。 我
我是一名优秀的程序员,十分优秀!