如何在选择时更改底部导航图标

How to change bottom navigation icons on selection

如何在选择和取消选择时更改底部导航图标 这是我之前的代码

bottomBar.getBar().setBackgroundColor(getResources().getColor(R.color.bottom_tabs));
bottomBar.setActiveTabColor("#FFFFFE");

首先你必须在你的 drawable 文件夹中选择 drawable

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/selected_icon"/>
    <item android:state_selected="false" android:drawable="@drawable/unselected_icon" />
    <item android:drawable="@drawable/unselected_icon" />
</selector>

在底部导航菜单中将这个可选择的可绘制对象作为图标放置

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/itemId"
        android:icon="@drawable/drawable_selectable_name"
        android:title="@string/title"/>
</menu>

在 activity 或您使用底部导航的片段中使用以下行

bottomNavigationView.itemIconTintList = null