如何在没有 ActionBar 的 AndroidX 中设置 BottomNavigationView

nge*_*nge 6 android android-fragments android-actionbar bottomnavigationview android-architecture-components

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

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)
Run Code Online (Sandbox Code Playgroud)

由于我希望拥有一些带有 ActionBar 的片段(例如 CollapsingToolbarLayout),因此如何设置底部导航以没有 actionBar?

小智 6

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

val appBarConfiguration = AppBarConfiguration(
    setOf(R.id.navigation_popular, R.id.navigation_top_rated, R.id.navigation_favorites)
)
setupActionBarWithNavController(navController, appBarConfiguration)
Run Code Online (Sandbox Code Playgroud)


小智 2

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

if (savedInstanceState == null) {
    setupBottomNavigationBar()

} // Else, need to wait for onRestoreInstanceState

supportActionBar?.hide()
Run Code Online (Sandbox Code Playgroud)