【问题标题】:How to hide the default fragment actionBar create our own actionBar in android & kotlin如何隐藏默认片段 actionBar 在 android & kotlin 中创建我们自己的 actionBar
【发布时间】:2021-11-06 20:18:27
【问题描述】:

我有一个底部导航的 android 应用程序,其中包含 3 个片段,并且默认情况下都有自己的 actionBar。我想隐藏三个片段的所有默认操作栏,并创建自己的带有许多选项的操作栏。我正在使用 Kotlin。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: android kotlin android-fragments android-actionbar android-architecture-navigation


【解决方案1】:

您只需要设置应用程序的样式。转到values/styles.xml,将AppTheme 的值编辑为NoActionBar。然后要在每个片段中创建自己的 AppBar,只需在您的 Xml 中使用 AppBarLayout。例如:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
       ...
 </style>

【讨论】:

  • 是的,我之前尝试过,但所有操作栏都连接到底部导航菜单,其中为每个片段显示默认操作栏。在我这样做之后,我的应用程序崩溃了,我认为我在这里遇到了错误。下一条评论分享代码
  • 覆盖 fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navView: BottomNavigationView = findViewById(R.id.nav_view) val navController = findNavController( R.id.nav_host_fragment) val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_home, R.id.navigation_messages, R.id.navigation_profile)) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController)
【解决方案2】:

这是一个快速的解决方案。

您找到 styles.xml 并将基本应用程序主题更改为“Theme.AppCompat.NoActionBar”,如下所示。

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
</style>

并在xml中定义动作栏的自定义布局

【讨论】:

    【解决方案3】:

    我已经解决了这个问题。

    val appBarConfiguration = AppBarConfiguration(setOf( R.id.navigation_home, R.id.navigation_messages, R.id.navigation_profile)) 
    setupActionBarWithNavController(navController, appBarConfiguration)
    

    只需从主要活动中删除上述两行,您的问题就解决了。

    【讨论】:

      【解决方案4】:

      首先,ActionBar 是活动属性,而不是片段。如果您想要自定义操作栏,请点击此链接,https://myandroid.site/custom-toolbar-with-style-in-kotlin-design-colorful-toolbar/

      您可以在 Activity 中拥有默认的 ActionBar,并创建您自己的布局,看起来像一个操作栏。

      隐藏Action Bar(下面的代码应该在Activity里面):

      if (supportActionBar != null)
          supportActionBar?.hide() 
      

      【讨论】:

      • 我想你不知道将操作栏连接到片段。
      • ActionBar/ToolBar 是 Activity 属性。您没有将 ActionBar 连接到片段,您只需通过 getActivity() 或 requireActivity() 方法在片段中访问它。
      猜你喜欢
      • 2015-07-04
      • 2023-03-17
      • 2016-04-06
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2019-03-05
      相关资源
      最近更新 更多