在底部导航栏图标的顶部显示徽章

新手上路,请多包涵

我已经在我的应用程序中实现了底部导航视图,并且我已经查看了在图标顶部显示徽章的每个位置,我想知道 是否甚至可以实现。任何帮助表示赞赏。谢谢你。

原文由 heisenberg91 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 427
2 个回答

现在本地支持添加徽章,使用最新的材料依赖项将其添加到您的 build.gradle

     implementation 'com.google.android.material:material:1.1.0-alpha09'

在你的布局中添加这个

<!-- The rest of your layout here ....-->

  <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/bottom_nav_menu"
            />

那么你可以

     val navBar  = findViewById<BottomNavigationView>(R.id.bottom_navigation)
     navBar.getOrCreateBadge(R.id.action_add).number = 2

您的 R.id.action_add 将是您要放置徽章的菜单项的 ID。从您提供给 BottomNavigationView 的菜单文件中检查它。

确保您的应用主题位于 Theme.MaterialComponents 中

您可以在样式或清单中检查它。对于这个例子,我的是这个

     <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor" tools:targetApi="lollipop">@color/colorPrimary</item>
    </style>

原文由 Abel 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您只想要没有数字的点,那么您只需使用 com.google.android.material.bottomnavigation.BottomNavigationView 和在 java 中创建底部导航

BottomNavigationView mBtmView = (BottomNavigationView) findViewById(R.id.bottom_navigatin_view);
mBtmView.setOnNavigationItemSelectedListener(this);
mBtmView.getOrCreateBadge(R.id.chatFragment).setBackgroundColor(getResources().getColor(R.color.Red));

在此处输入图像描述

原文由 Mats 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题