【问题标题】:How to Set BottomNavigationView in AndroidX with No ActionBar如何在没有 ActionBar 的 AndroidX 中设置 BottomNavigationView
【发布时间】:2019-10-27 05:05:11
【问题描述】:

我在 Android 上使用具有 Single Activity 模式的 Android 架构组件。我使用的导航模式是 BottomNavigationView。我实际上希望父活动没有 ActionBar,但将我的主题设置为 NoActionBar 类型会使应用程序崩溃。在 Activity 中设置 Navigation 如下所示

val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
    setOf(R.id.navigation_popular, R.id.navigation_top_rated, R.id.navigation_favorites)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)

我如何设置底部导航以不使用 actionBar,因为我希望有一些带有 ActionBar 的片段,例如 CollapsingToolbarLayout?

【问题讨论】:

  • setting my theme to be of type NoActionBar crashes the App您是如何更改主题的,或者尝试这样做?

标签: android android-fragments android-actionbar android-architecture-components bottomnavigationview


【解决方案1】:

我认为您找到了解决方案,但对于其他人来说, 如果您想将 BottomNavigationView 与 .NoActionBar 主题一起使用,那么您应该删除这些行:

val appBarConfiguration = AppBarConfiguration(
    setOf(R.id.navigation_popular, R.id.navigation_top_rated, R.id.navigation_favorites)
)
setupActionBarWithNavController(navController, appBarConfiguration)

【讨论】:

  • 这应该是一个可以接受的答案。非常感谢队友。
  • 这会导致内存泄漏,因为每次你点击底部导航中的一个项目时,都会创建一个新的片段实例,同时保持旧的片段存活。
【解决方案2】:

使用 Android 导航 BottomNavigationView。不要在清单中使用 NoActionBar 限制活动。而是从您的活动中检索支持操作栏的实例并使用其公共方法hide()

if (savedInstanceState == null) {
    setupBottomNavigationBar()

} // Else, need to wait for onRestoreInstanceState

supportActionBar?.hide()

【讨论】:

    猜你喜欢
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2019-07-11
    • 2017-08-31
    • 2023-01-03
    • 2023-03-06
    • 2021-01-31
    相关资源
    最近更新 更多