我正在BadgeDrawable
使用BottomNavigationView
. 但是当我将徽章放在底部导航视图的图标中时,我得到了这个异常
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f0400db a=6}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:597)
at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:76)
at com.google.android.material.resources.TextAppearance.<init>(TextAppearance.java:85)
at com.google.android.material.badge.BadgeDrawable$SavedState.<init>(BadgeDrawable.java:222)
at com.google.android.material.badge.BadgeDrawable.<init>(BadgeDrawable.java:424)
at com.google.android.material.badge.BadgeDrawable.createFromAttributes(BadgeDrawable.java:332)
at com.google.android.material.badge.BadgeDrawable.create(BadgeDrawable.java:300)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.getOrCreateBadge(BottomNavigationMenuView.java:692)
at com.google.android.material.bottomnavigation.BottomNavigationView.getOrCreateBadge(BottomNavigationView.java:689)
at com.app.UI.MainActivity$1.onNavigationItemSelected(MainActivity.java:129)
at com.google.android.material.bottomnavigation.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:241)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:131)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
我在请求成功时的Observe
回调中设置徽章的数量。LiveData
这是我的看法
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation"
android:background="@android:color/transparent"
app:menu="@menu/bottom_navigation_menu"
app:itemBackground="@drawable/navigation_background_selector"
app:itemIconTint="@color/text_color_drawer_selector"
android:padding="5dp"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
/>
我如何设置徽章的编号
int menuItemId = bottom_navigation.getMenu().getItem(1).getItemId();
BadgeDrawable badge = bottom_navigation.getOrCreateBadge(menuItemId);
badge.setNumber(20);
我的风格
<style name="Theme.app" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorAccent">@color/colorAccent</item>
</style>