如何更改Android中BottomNavigationView中的所选图标图像?

时间:2017-05-19 09:57:44

标签: android material-design

我已将menu_main.xml夸大到BottomNavigationView,现在我想在选择项目时更改BottomNavigation中的图标。

4 个答案:

答案 0 :(得分:3)

如果以上解决方案无法让您更改所选项目图标,请在代码中添加以下行:

bottomNavigationView.setItemIconTintList(null);

这将禁用所选项目图标的颜色色调效果,并根据您的选择器drawable更改图标。

我遇到了同样的问题。我添加了选择器drawable,用于在选中/选中时更改BottomNavigationView项目的图标。

答案 1 :(得分:1)

您必须将其保存在res/drawable目录中。

并按如下所示使用它:

例如:bottomNavigationMenu.xml ...

android:icon="@drawable/your_selector"

答案 2 :(得分:0)

drawable文件夹中创建名为selector的项目,可以根据所使用的小部件的选定状态更改图标

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

使用selector作为导航中的菜单图标

答案 3 :(得分:0)

    Xml of Bottom NavigationView 

        <android.support.design.widget.BottomNavigationView
                        android:id="@+id/bottom_navigation"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:itemIconTint="@drawable/selector_bottom_navigation"
                        app:itemTextColor="@drawable/selector_bottom_navigation"
                        app:menu="@menu/bottom_navigation" />

    Bottm Navigation Menu 

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/action_home"
            android:icon="@drawable/selecter_tab_home"
            android:title="@string/text_home"
            app:showAsAction="ifRoom" />
        <item
            android:id="@+id/action_explore"
            android:icon="@drawable/selector_tab_explore"
            android:title="@string/text_explore"
            app:showAsAction="ifRoom" />

        <item
            android:id="@+id/action_profile"
            android:icon="@drawable/selector_tab_profile"
            android:title="@string/text_profile"
            app:showAsAction="ifRoom" />

    </menu>

Selector of home menu you can put next two same also with differnt drawable file 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_tab_home" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_tab_home_fill" android:state_checked="true"/>

</selector>
相关问题