Android BottomNavigationView底部导航栏的使用

  <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;
            }
        });

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值