- rely
implementation 'com.google.android.material:material:1.0.0'
- First of all
notifications_badge.xml
, I need to write a badge layout.The unit that I used to adapt the pagept
can also be replaceddp
, depending on the project.
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/notifications_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="26pt"
android:layout_marginTop="14pt"
android:background="@drawable/notification_badge"
android:gravity="center"
android:text="1+"
android:textColor="@color/white"
android:textSize="16pt" />
</merge>
notification_badge
It is shape
painted with red dots
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="28pt"
android:height="28pt" />
<solid android:color="@color/read" />
</shape>
The height of my navigation bar is 100pt, so the dot diameter is 26pt, which is nearly one-third of the height of the navigation bar. Then the navigation bar picture is 75px wide, so android:layout_marginLeft="26pt"
you can make the small red dot as close to the right side of the ItemView as possible. You can debug it yourself Clear.
- Then
stackoverflow
the answer is encapsulated into a simple tool class, as follows.
public class BNVNotifacationBadgeUtil {
/**
* 添加消息徽章
*
* @param itemIndex 表示添加到第几个ItemView
* @param desc 每个徽章上的内容是啥
*/
public static void addNotificationBadge(BottomNavigationView mBNV, int itemIndex, String desc) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
LayoutInflater.from(mBNV.getContext()).inflate(R.layout.layout_notification_badge, mCurrentItemView, true);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
mBadge.setText(desc);
}
}
/**
* 移除消息徽章
*
* @param itemIndex
*/
public static void removeNotificationBadge(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
((ViewGroup) mBadge.getParent()).removeView(mBadge);
}
}
/**
* 修改消息徽章上面的内容
*
* @param itemIndex
* @param desc
*/
public static void modifyNotificationBadgeContent(BottomNavigationView mBNV, int itemIndex, String desc) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
mBadge.setText(desc);
}
}
/**
* 判断当前索引对应的ItemView中是否有徽章
*
* @param mBNV
* @param itemIndex
*/
public static Boolean judgeBadgeExist(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge == null) {
return false;
} else {
return true;
}
}
/**
* 获取当前索引对应的ItemView对象
*
* @param mBNV
* @param itemIndex
* @return
*/
private static BottomNavigationItemView getBottomNavigationItemView(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationMenuView bottomNavigationMenuView = (BottomNavigationMenuView) mBNV.getChildAt(0);
return (BottomNavigationItemView) bottomNavigationMenuView.getChildAt(itemIndex);
}
}