先放效果
遇到的问题
图片和文字颜色没有同步
如何解决
先上代码
没有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);