gpt4 book ai didi

java - Android通过程序在bottomNavigationBar中设置图标和文本的颜色失败

转载 作者:行者123 更新时间:2023-12-04 08:59:51 24 4
gpt4 key购买 nike

我创建了一个 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/bottomNavigationBarCheckedNormal" />

<item
android:state_pressed="true"
android:color="@color/bottomNavigationBarCheckedNormal" />

<item
android:color="@color/bottomNavigationBarUncheckedNormal" />
</selector>
当我在我的 Activity xml 文件中设置它时它是有效的,如下所示:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/activity_news_bottom_navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
app:menu="@menu/bottom_navigation_menu"
app:itemIconTint="@drawable/custom_bottom_navigation_normal"
app:itemTextColor="@drawable/custom_bottom_navigation_normal"
app:labelVisibilityMode="labeled" />
但是我想通过程序来改变它,所以我尝试使用它:
bottomNavigationView.setBackgroundColor(Color.parseColor(AppColor.bottomNavigationBarBackgroundColor));
bottomNavigationView.setItemIconTintList(ColorStateList.valueOf(R.drawable.custom_bottom_navigation_normal));
bottomNavigationView.setItemTextColor(ColorStateList.valueOf(R.drawable.custom_bottom_navigation_normal));
但是不行,选择和取消选择时图标和文本颜色没有区别,颜色不是我设置的。怎么解决,谢谢。

最佳答案

你应该使用:

bottomNavigationView.setItemTextColor(
AppCompatResources.getColorStateList(this,R.drawable.custom_bottom_navigation_normal));
方法 valueOf :

Returns A ColorStateList containing a single color. This value cannot be null.

关于java - Android通过程序在bottomNavigationBar中设置图标和文本的颜色失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63614032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com