如何在bottomNavigationView android项目上显示徽章?
我看到了这个问题,决定将我自己的自定义徽章添加到我的bottomNavigationView中。首先,我创建了我的徽章的特殊布局:如何在bottomNavigationView android项目上显示徽章?,android,bottomnavigationview,Android,Bottomnavigationview,我看到了这个问题,决定将我自己的自定义徽章添加到我的bottomNavigationView中。首先,我创建了我的徽章的特殊布局: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <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个项目时,收到以下错误:
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
}
对于那些无法访问
GetorCreateBadge
的用户,另一种方法是使用以下方法
Theme.MaterialComponents.Light.darkaActionBar
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));
也许您应该更改
menuView.getChildAt(1)代码>?您可以在底部导航中添加自定义布局menu@JohnJoe,当我更改此项时,在BottomNavigationItemView itemView=(BottomNavigationItemView)菜单视图中收到错误。getChildAt(0)代码>@Hussainabas,我浏览了所有可能的库,但后来我决定创建自己的库badge@AndrewGoroshko试试john joe的回答,我如何才能为更多的项目添加徽章,而不仅仅是一个项目?然后你需要另一个自定义布局。因此,如果我有5个项目,我必须使用5个不同的布局?不确定这是不是最好的方法,但这对我来说很有效。我在我的项目中使用java:(你能添加java代码吗?我想在更多项目上添加徽章。是的!!如果你给一些时间进行基本语法,你可以理解kotlin代码,如果你想获得更多徽章,需要为特定的BottomNavigationItemView项目增加更多徽章项目
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));