BottomNavigationView使用中注意事项

设置选中和未选中字体颜色

<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;
    };
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值