我看到了这个问题 - Display badge on top of bottom navigation bar's icon并决定将我自己的自定义徽章添加到我的bottomNavigationView中。首先,我创建了徽章的特殊布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/counter_badge"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="top|center_horizontal"
android:layout_marginStart="20dp"
android:gravity="center"
android:textSize="12sp"
android:textStyle="bold"
android:ellipsize="end"
android:textAlignment="center"
android:textColor="@color/white"
android:padding="3dp"
android:background="@drawable/badge"/>
</FrameLayout>
然后我在我的活动中为徽章textView添加了一些信息:
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(0);
notificationBadge = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false);
TextView textView = notificationBadge.findViewById(R.id.counter_badge);
textView.setText("15");
itemView.addView(notificationBadge);
但是当我看到我只能使用我的视图中的一个项目时,当我尝试从0更改为1项时,我收到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.internal.BottomNavigationMenuView.getChildAt(int)' on a null object reference
然后我尝试使用特殊的项目ID,但我收到类似的错误。也许smb知道如何为我想要的物品添加徽章?
您应该将代码更改为此
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(1);
编辑
要拥有更多具有徽章的项目,您需要创建另一个布局,依此类推
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(0);
notificationBadge = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false);
TextView textView = notificationBadge.findViewById(R.id.counter_badge);
textView.setText("15");
itemView.addView(notificationBadge);
BottomNavigationMenuView menuView1 = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView1 = (BottomNavigationItemView) menuView1.getChildAt(1);
notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge_one, menuView1, false);
TextView textView = notificationBadgeOne.findViewById(R.id.counter_badge);
textView.setText("15");
itemView1.addView(notificationBadgeOne);
只需在BottomNavigationView上添加徽章即可:
private lateinit var bottomNavItemView: BottomNavigationItemView
private var messageBadgeView: View? = null
val mBottomNavigationMenuView = getChildAt(0) as BottomNavigationMenuView
val view = mBottomNavigationMenuView.getChildAt(1)
bottomNavItemView = view as BottomNavigationItemView
messageBadgeView = LayoutInflater.from(this@Activity)
.inflate(R.layout.item_message_count_badge,
mBottomNavigationMenuView, false)
messageBadgeView!!.badgeCount.text = "99+"
//Add badge
bottomNavItemView.addView(messageBadgeView)
//Or Remove badge
bottomNavItemView.removeView(messageBadgeView)