简体   繁体   English

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

[英]How to change bottom navigation icons on selection

How to change bottom navigation icons on selection and unselection This was my previous code如何在选择和取消选择时更改底部导航图标这是我以前的代码

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

First you have to make drawable selectable in your drawable folder首先,您必须在可绘制文件夹中选择可绘制

<?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>

In the bottom navigation menu place this selectable drawable as icon在底部导航菜单中将这个可选择的可绘制对象放置为图标

<?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>

In activity or fragment where you have used bottom navigation use the following line在您使用底部导航的活动或片段中使用以下行

bottomNavigationView.itemIconTintList = null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM