问题描述
我只想在应用程序中为BottomNavigationView
的menuItem
添加徽章.我使用的是材料组件库(版本1.1.0-alpha08)的BottomNavigationView
,因为它的最新版本是在7天前发布的,所以我没有找到相同的任何教程,因为在此进行了更改BottomNavigationView
的showBadge方法的版本,我们不能使用该方法.
I just want to add badge for a menuItem
of BottomNavigationView
in my app. I'm using BottomNavigationView
of Material Components library(version 1.1.0-alpha08) since its the latest version released just 7 days ago from now I didn't found any tutorial for the same, Now because there are changes made in this version of BottomNavigationView
's showBadge method we cannot use that method.
我尝试通过BottomNavigationView
的实例调用getBadge
和getOrCreateBadge
方法.
I've tried calling getBadge
and getOrCreateBadge
method over instance of 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);
我是android开发领域的菜鸟,所以如果有人可以提供详细解决此问题的方法,这将非常感激我.
I'm noob in android development so if anyone can provide solution for this problem in detail that would be very grateful to me.
推荐答案
- 首先将您的项目迁移到androidX. 如何迁移
- 在您的
build.gradle
中包括依赖项: - First migrate your project to androidX. How to migrate
- Include dependency in your
build.gradle
: - 您的基本AppLevel主题应使用
Material Component Theme
,例如: - Your Base AppLevel theme should use
Material Component Theme
like:
implementation 'com.google.android.material:material:version'
获取版本
您的应用级主题
<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>
活动代码:
val menuItemId: Int = btm_nav.menu.getItem(0).itemId //0 menu item index.
badgeDrawable = btm_nav.getOrCreateBadge(menuItemId)
badgeDrawable.isVisible = true
badgeDrawable.number = 10
badgeDrawable.badgeGravity = BadgeDrawable.TOP_END //badge gravity
//BadgeDrawable.TOP_START
//BadgeDrawable.BOTTOM_END
//BadgeDrawable.BOTTOM_START
badgeDrawable.isVisible = false //hide badge
badgeDrawable.clearNumber()
XML布局:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/btm_nav"
style="@style/Widget.Design.BottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav_menu"/>
这篇关于如何显示材质库(版本1.1.0-alpha08)的BottomNavigationView的menuItem的标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!