BottomNavigationView图标和文字颜色同步问题

这篇博客主要介绍了在Android开发中遇到BottomNavigationView的图标和文字颜色无法同步的问题及其解决方案。作者提供了代码示例,包括XML布局文件和Java代码,强调了'-android'前缀在设置未选中状态时的重要性。

先放效果




遇到的问题

图片和文字颜色没有同步

如何解决

先上代码
没有menu文件的自己在res目录下创建

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home"
        android:icon="@drawable/icon_home_default"
        android:title="KAWA"
        />
    <item
        android:id="@+id/my"
        android:icon="@drawable/icon_mine_default"
        android:title="我的"
        />
</menu>

图片的话可以直接放入没有颜色的图片,这里不涉及选择器,具体的图片和文字颜色代码中设置
对应的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/fl_home"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"/>


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bnv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/navigationview_home"
        />


</LinearLayout>

java代码

    @BindView(R.id.bnv)
    BottomNavigationView mBnv;

   int[][] states = new int[][]{
                new int[]{-android.R.attr.state_checked},
                new int[]{android.R.attr.state_checked}
        };

        int[] colors = new int[]{getResources().getColor(R.color.home_bottom_normal),
                getResources().getColor(R.color.home_bottom_checked)
        };
        ColorStateList csl = new ColorStateList(states, colors);
        mBnv.setItemTextColor(csl);
        mBnv.setItemIconTintList(csl);

这里需要注意的是中的"-android"的"-"不能省略,这应该表达的意思是未选中的状态.
还有一种方式,等一会儿上代码...
希望文章能帮到你









评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值