如何在 bottomNavigationView android 的项目上显示徽章?
How to display badge on items of bottomNavigationView android?
我看到了这个问题 - 并决定将我自己的自定义徽章添加到我的 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>
然后我在我的 activity:
处向徽章 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)
现在无需自定义视图即可使用
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
}
无法访问 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));
你可以开始了!示例输出如下
我看到了这个问题 -
<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>
然后我在我的 activity:
处向徽章 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)
现在无需自定义视图即可使用
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
}
无法访问 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));
你可以开始了!示例输出如下