设置选中和未选中字体颜色
<com.google.android.material.bottomnavigation.BottomNavigationView
............
android:theme="@style/bottom_menu_color"/>
<style name="bottom_menu_color">
// 选中图标颜色
<item name="colorPrimary">@android:color/holo_red_light</item>
// 未选中图标颜色
<item name="android:colorSecondary" tools:targetApi="n_mr1">@android:color/holo_purple</item>
// 选中字体颜色
<item name="android:textColorPrimary">@android:color/holo_red_light</item>
// 选中字体颜色
<item name="android:textColorSecondary">@android:color/holo_purple</item>
</style>
设置未选中字体是否显示
<com.google.android.material.bottomnavigation.BottomNavigationView
............
app:labelVisibilityMode="labeled"/>
按钮设置
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_notifications_black_24dp" android:state_checked="false"/>
<item android:drawable="@drawable/ic_notifications_red_24dp" android:state_checked="true"/>
</selector>
选中和未选中字体大小设置,如果设置相同大小可避免上下位移动画效果
<resources>
<dimen name="design_bottom_navigation_text_size">13sp</dimen>
<dimen name="design_bottom_navigation_active_text_size">13sp</dimen>
</resources>
图标切换需要配置
protected void onCreate(@Nullable Bundle savedInstanceState) {
··············
bottomNavigationView.setItemIconTintList(null);
}
BottomNavigationView切换监听点击(判断一下是否是当前fragment)
protected void onCreate(@Nullable Bundle savedInstanceState) {
··············
bottomNavigationView.setOnItemSelectedListener(itemSelectedListener);
}
int checked = 1;
private final NavigationBarView.OnItemSelectedListener itemSelectedListener = item -> {
switch (item.getItemId()) {
case R.id.fragment_one:
if (checked != 1) {
controller.popBackStack();
controller.navigate(R.id.fragment_one);
checked = 1;
}
break;
case R.id.fragment_two:
if (checked != 2) {
controller.popBackStack();
controller.navigate(R.id.fragment_two);
checked = 2;
}
break;
}
return true;
};