自定义图标 - 底部导航视图

Ani*_*ita 2 android android-layout bottomnavigationview android-bottomnav

我有一个 BottomNavigationView 并且想要为选定和未选定状态使用自定义图标。尝试使用选择器但它不起作用。

知道如何放置自定义图标吗?

编辑- 将评论中的代码添加到问题中

<item 
    android:id="@+id/navigation_card" 
    android:icon="@drawable/iv_home_card" 
    app:itemBackground="@drawable/navigation_card" 
    app:showAsAction="ifRoom" tools:ignore="MenuTitle" 
/>
Run Code Online (Sandbox Code Playgroud)

像这样我添加了图标。现在对于选定状态,它用主题颜色描边,但我想用另一个图标替换图标。

我尝试制作选择器但没有用

<selector 
    xmlns:android="schemas.android.com/apk/res/android"> 

    <item android:state_checked="false" 
        android:drawable="@drawable/btn_star_off_normal" /> 

    <item android:state_checked="true" 
        android:drawable="@drawable/btn_star_on_normal" /> 
</selector>
Run Code Online (Sandbox Code Playgroud)
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { 
    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
        switch (item.getItemId()) { } 
        return false; 
    } 
};
Run Code Online (Sandbox Code Playgroud)

use*_*604 7

You can simply create drawable selector in drawable folder and image can be change according to the state of the widget used in view

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_active" android:state_checked="true"/>
<item android:drawable="@drawable/home_inactive" android:state_checked="false"/>
Run Code Online (Sandbox Code Playgroud)

and set this selector in bottom nav menu file

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/nav_home_selector"
    android:title="@string/tab_home_title" />
Run Code Online (Sandbox Code Playgroud)

Make sure to add this line to your java file. This solution will not work for selected icon unless you add this line.

bottomNavigationView.setItemIconTintList(null);
Run Code Online (Sandbox Code Playgroud)

This will disable tint effect of selected item icon.