<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
app:labelVisibilityMode="auto"
app:menu="@menu/nav_menu" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/tab_conversation"
android:icon="@drawable/em_main_tab_conversation"
android:title="会话" />
<item
android:id="@+id/tab_friends"
android:icon="@drawable/em_main_tab_friends"
android:title="通讯录" />
<item
android:id="@+id/tab_mine"
android:icon="@drawable/em_main_tab_me"
android:title="我的" />
</menu>
有一项要注意:
在BottomNavigationView的源码内有如下一段代码 (意思是:) menu文件内添加的item个数大于5个时会报错
常见的效果显示
1.去除点击时出现的水波纹背景效果
app:itemBackground="@null"
2.去除点击时的动画效果以及位移现象
app:labelVisibilityMode="labeled"
属性取值有labeled、unlabeled、selected、auto
labeled:所有按钮中都显示图标与标签。
unlabeled:所有按钮中都不显示标签,只显示图标。
selected:只有选中的按钮才显示标签与图标,其他未选中的只显示图标。
auto:自动模式,该模式使用item数来确定是否显示或隐藏标签。当按钮个数小于等于3个时使用labeled模式,大于3个时使用selected模式。
3.去除点击时默认的图标颜色修改
bottomNavigationView.itemIconTintList = null
4.修改文字大小
在资源文件dimens.xml中添加
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
<dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>
在Activity中使用
mainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.tab_conversation:
mainVp.setCurrentItem(0);
break;
case R.id.tab_friends:
mainVp.setCurrentItem(1);
break;
case R.id.tab_mine:
mainVp.setCurrentItem(2);
break;
default:
break;
}
return true;
}
});