简体   繁体   English

如何更改android studio底部导航栏上选择的图标颜色

[英]How to change the icon color selected on bottom navigation bar in android studio

When I select an item in bottom navigation bar in android studio, background item selected is equal to primarycolor in values->colors.xml.当我在 android 工作室的底部导航栏中 select 一个项目时,选择的背景项目等于 values->colors.xml 中的原色。 and now I want to change this color which is not to same the primarycolor.现在我想改变这种与原色不同的颜色。 how do i can to change it?我怎样才能改变它?

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        Fragment fragment;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_addpost:
                    fragment = new AddFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_notifications:
//                    mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.navigation_profile:
                    fragment = new ProfileFragment();
                    loadFragment(fragment);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        loadFragment(new HomeFragment());
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setItemTextColor(ColorStateList.valueOf(Color.RED));
    }

To change the selected tab icon color in BottomNavigationView you should use the Selector.要更改BottomNavigationView选定的选项卡图标颜色,您应该使用选择器。

Create bottom_navigation_selector.xml创建bottom_navigation_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:color="@color/yourSelectedColor" />
  <item android:color="@color/defaultColor"  />
</selector>

Apply app:itemIconTint="@drawable/bottom_navigation_selector" to your BottomNavigationView in xml file.app:itemIconTint="@drawable/bottom_navigation_selector"xml文件中的 BottomNavigationView。

Despite reading all the answers I was confused about whole process, so I am going to explain step by step how I resolved this issue so beginners can understand it properly尽管阅读了所有答案,但我对整个过程感到困惑,所以我将逐步解释我是如何解决这个问题的,以便初学者能够正确理解它

Let's assume you created bottom navigation activity with name MainActivity so now假设您现在创建了名为MainActivity底部导航活动

create bottom_navigation_selector.xml in your drawable folder using this code使用此代码在您的drawable文件夹中创建bottom_navigation_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:color="@color/yourSelectedColor" />
  <item android:color="@color/defaultColor"  />
</selector>

then go to the activity_main.xml layout and add this line in BottomNavigationView然后转到activity_main.xml布局并在BottomNavigationView添加这一行

app:itemIconTint="@drawable/bottom_navigation_selector"

If you also want to change text color accordingly then you need to add this line aswell如果您还想相应地更改文本颜色,则还需要添加此行

app:itemTextColor="@drawable/bottom_navigation_selector"
    

To show the real color of items use this要显示项目的真实颜色,请使用此

java java

bottom_navigation.setItemIconTintList(null);

kotlin kotlin

bottom_navigation.itemIconTintList = null

and if you want to change the calor just replace the null with如果你想改变热量,只需将 null 替换为

Color.parseColor("#ffffff")

Try,尝试,

navigation.setItemIconTintList(Color.BLUE);

Update :更新 :

navigation.setItemIconTintList(Color.parseColor("#fafafa"));
bottomNavigationView.setItemIconTintList(ColorStateList.valueOf(Color.parseColor("#3F51B5")));

If you are using compose and constructing a BottomNavigationItem you have to use the u nselectedContentColor option to set the color.如果您正在使用 compose 并构造一个BottomNavigationItem ,则必须使用unselectedContentColor选项来设置颜色。

eg例如

unselectedContentColor = MaterialTheme.colors.primary,
   BottomNavigationItem(
                icon = {},
                unselectedContentColor = MaterialTheme.colors.primary,
                alwaysShowLabel = false,
                selected = currentRoute == item.route,
                onClick = { },
            )

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

相关问题 选中时如何更改导航栏元素的颜色 ANDROID STUDIO - How to change color of navigation bar elements when they are selected ANDROID STUDIO 如何在Android中更改屏幕底部的软导航背景和图标颜色? - How to change the bottom on screen soft navigation background and icon color in android? 如何在 Android Studio 中更改底部导航栏按钮的点击波纹效果颜色 - How to change on tap ripple-effect color of bottom-navigation-bar buttons in Android Studio 在底部导航栏中,即使我选择了该图标,该图标也不会突出显示。如何更改它? - In Bottom Navigation Bar , icon is not getting highlighted even after i selected that icon .How to change it? 在 Android 上更改导航栏图标颜色 - Change navigation bar icon color on Android 如何在android studio的底部导航菜单中更改片段和选定项目 - How to change fragment and selected item in bottom navigation menu in android studio Android Studio 底部导航栏颜色怎么改? - how to change the colour of bottom navigation bar in Android Studio? Android studio - 更改 NOT OF BOTTOM NAVIGATION VIEW 的颜色 - Android studio - change color of NOT OF BOTTOM NAVIGATION VIEW 如何在android studio中更改导航视图中汉堡包图标的颜色 - how to change the color of the hamburger icon in the navigation view in android studio 底部导航图标颜色变化 - Bottom Navigation Icon color change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM