我正在尝试以编程方式更新 ImageView 的色调,并且我使用的颜色是一个选择器,在启用或禁用 View 时具有不同的颜色。当我尝试使用其他 StackOverflow 帖子中的建议方法时,它设置了启用的颜色,但不会更新禁用的 View
例如,我有以下四个点,它们在 XML 中为红色,而我在代码中将底部的两个点设置为绿色。右侧的两个点被禁用,因此我希望两者都是相同的禁用灰色,但正如您所看到的,以编程方式设置的颜色始终为绿色
如何以编程方式更新 ImageView 的色调,使其正确遵循选择器中的状态?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Also tried making these AppCompatImageViews -->
<ImageView
android:id="@+id/imgDot1"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
<ImageView
android:id="@+id/imgDot2"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgDot3"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
<ImageView
android:id="@+id/imgDot4"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:enabled="false"
android:src="@drawable/ic_dot" />
</LinearLayout>
imgDot2.isEnabled = false
imgDot4.isEnabled = false
// Tried each of these in turn
ImageViewCompat.setImageTintList(imgDot3, ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful)))
ImageViewCompat.setImageTintList(imgDot4, ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful)))
imgDot3.setColorFilter(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot4.setColorFilter(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot3.imageTintList = ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful))
imgDot4.imageTintList = ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful))
imgDot3.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot4.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color_green_stateful))
color_red_stateful.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#999"/>
<item android:color="#F00"/>
</selector>
color_green_stateful.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#999"/>
<item android:color="#0F0"/>
</selector>
ic_dot.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#000"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>
最佳答案
您在每个尝试的行中都使用了 getColor
方法,但这不正确,这不是颜色,而是 selector
(color_red_stateful
和color_green_stateful
文件)。使用 Resources
或最好使用 ContextCompat
类及其有用的方法 getColorStateList
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);
此 colorStateList
应设置为所需 ImageView
的 imageTintList
关于android - 以编程方式为 ImageView 设置有状态色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67799722/