如何更改BottomNavigationView徽章背景的颜色?

Zer*_*ool 8 android material-design bottomnavigationview material-components material-components-android

我已经BottomNavigationView根据这个例子实现了徽章计数 /sf/answers/3943845041/

它工作正常,但我想更改徽章的背景颜色有什么办法吗?

Gab*_*tti 20

就用setBackgroundColor()徽章的方法吧。

  BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
  badge.setNumber(..);
  badge.setBackgroundColor(....);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果您想在您的应用程序中全局更改样式,您可以badgeStyle在应用程序主题中定义attr:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
  <item name="badgeStyle">@style/CustomBadge</item>
  ...
</style>

<style name="CustomBadge" parent="@style/Widget.MaterialComponents.Badge">
   <item name="backgroundColor">@color/.....</item>
</style>
Run Code Online (Sandbox Code Playgroud)