我看到了这个问题-Display badge on top of bottom navigation bar's icon,并决定将自己的自定义徽章添加到我的bottomNavigationView中。首先,我为徽章创建了特殊的布局:
Debug > Windows > Exception Settings
然后我在活动时向徽章textView添加一些信息:
<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>
但是,正如我所见,我只能处理视图中的一项,但是当我尝试从0更改为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);
然后,我尝试使用特殊的商品ID,但收到类似的错误。也许smb知道如何为我想要的物品添加徽章?
答案 0 :(得分:1)
您应该将代码更改为此
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);
答案 1 :(得分:1)
只需在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)
答案 2 :(得分:1)
现在无需使用自定义视图就可以实现
com.google.android.material:material:1.1.0-alpha06
和更高版本
bottomNavigationView.getOrCreateBadge(R.id.bottom_navigation_view_item).apply {
backgroundColor = Color.RED
badgeTextColor = Color.WHITE
maxCharacterCount = 3
number = 0
isVisible = true
}
答案 3 :(得分:0)
无法访问getOrCreaeteBadge
的人的另一种方法是使用下面的方法;
确保底部导航上的主题设置为:
Theme.MaterialComponents.Light.DarkActionBar
然后,现在:
String messages = getIntent().getStringExtra("messages_count");
messages = messages.substring(10,11);
Log.d("MessagesCount", "getIncomingIntent: "+messages);
navigation.showBadge(R.id.bottom_messages).setNumber(Integer.parseInt(messages));
你很好!示例输出如下