尝试默认BottomNavigationView两种颜色(对于菜单项的两个状态)添加第三个条件和第三个颜色。为此,在drawable中创建了一个带有选择器的文件“bottom_nav_colors.xml”:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked = "true"
android:color = "@color/colorPrimaryDark"/>
<item android:state_activated="true"
android:color = "@color/colorAccent"/>
<item android:color = "@color/colorGray" />
</selector >
带有布局模板“activity_main.xml”的文件指出:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation_menu"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"/>
但是,我无法弄清楚类“MainActivity.java”(继承自类“MvpAppCompatActivity”)的类“BottomNavigationView”的哪个方法将其激活为我的菜单项“激活”(或任何其他来自列表:“加速”,“活跃”等。)。
或者如果你不能那样做,那该怎么办?
答案 0 :(得分:1)
if (ServiceApp.getCartsGoodsNumber() > 0) {
bnve.getBottomNavigationItemView(i).setIconTintList(
ContextCompat.getColorStateList(
bnve.getContext(),
color.bottom_select_nav_colors));
addBadgeAt(bnve, i, ServiceApp.getCartsGoodsNumber());
} else {
bnve.getBottomNavigationItemView(i).setIconTintList(null);
if (mNavigation_20Badge != null) mNavigation_20Badge.hide(true);
}
这里有一个类“BottomNavigationViewExe”的实例,方法addBadgeAt()如下所示:
private void addBadgeAt(BottomNavigationViewEx bnve,
final int position, final int number) {
// add badge
mNavigation_20Badge = new QBadgeView(bnve.getContext())
.setBadgeNumber(number).setGravityOffset(12, 2, true)
.bindTarget(bnve.getBottomNavigationItemView(position));
}
此方法可让您添加徽章图标: