如何显示材质库(版本1.1.0-alpha08)的BottomNavigationView的menuItem的徽标?

Shu*_*shi 5 android material-components material-components-android

我只想补充徽章了menuItemBottomNavigationView我的应用程序。我使用的BottomNavigationView是材料组件库(版本1.1.0-alpha08),因为它的最新版本是在7天前发布的,所以我没有找到与此相同的任何教程,因为该版本的BottomNavigationView“的showBadge方法,我们不能使用该方法。

我试过了的实例调用getBadgegetOrCreateBadge方法BottomNavigationView

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
    if (bottomNavigationView.getBadge(3) == null) {
        audioPlayingCountBadge = bottomNavigationView.getOrCreateBadge(3);
        audioPlayingCountBadge.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
    } else {
        audioPlayingCountBadge = bottomNavigationView.getBadge(3);
    }
    audioPlayingCountBadge.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

我在android开发中是菜鸟,所以如果有人可以提供详细解决此问题的方法,这将非常感激我。

Sum*_*kla 7

  • 首先将您的项目迁移到androidX。如何迁移
  • 在您的中包含依赖项build.gradle

implementation 'com.google.android.material:material:version' 获取版本

  • 您的基本AppLevel主题应使用Material Component Theme如下形式:

您的应用级主题

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

活动代码:

int menuItemId = btmNavView.getMenu().getItem(0).getItemId();  //0 menu item index.  
BadgeDrawable badgeDrawable = btmNavView.getOrCreateBadge(menuItemId);
badgeDrawable.setVisible(true);  
badgeDrawable.setNumber(1);   //1 represents badge value.
Run Code Online (Sandbox Code Playgroud)

XML布局:

 <com.google.android.material.bottomnavigation.BottomNavigationView
      style="@style/Widget.Design.BottomNavigationView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:menu="@menu/bottom_nav_menu"/>
Run Code Online (Sandbox Code Playgroud)

  • 废话谢谢,我想知道当我从1.1.0alpha07升级时showBadge发生了什么 (2认同)