[英]How to Change The Checked Tint Color of a CheckedTextView with Hex Value
我想在checked
视图状态时动态更改CheckedTextView
的色调颜色。 我很确定我可以通过在CheckedTextView
上调用setCheckMarkTintList
来实现这CheckedTextView
。 为此,我需要一个ColorStateList
,但问题是我想保留CheckedTextView
每个状态的所有颜色,除了checked
状态。
因此,我可以获得ColorStateList
的CheckedTextView
,但我不知道如何仅更改checked
状态的颜色。 我知道我可以创建一个新的ColorStateList
,但是如何确保它保留原始值的所有值?
我可以创建这样的状态列表:
int[][] states = new int[][] {
new int[]{android.R.attr.state_pressed},
new int[]{-android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{-android.R.attr.state_focused},
new int[]{android.R.attr.state_selected},
new int[]{-android.R.attr.state_selected},
new int[]{android.R.attr.state_checkable},
new int[]{-android.R.attr.state_checkable},
new int[]{android.R.attr.state_checked},
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_window_focused},
new int[]{-android.R.attr.state_window_focused},
new int[]{} // default state
}
并从原始ColorStateList
的颜色创建一个颜色列表:
int[] colors = new int[] {
stateList.getColorForState(new int[]{android.R.attr.state_pressed}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_pressed}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{android.R.attr.state_focused}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_focused}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{android.R.attr.state_selected}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_selected}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{android.R.attr.state_checkable}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_checkable}, stateList.getDefaultColor()),
Color.parseColor(colorHexValue),
stateList.getColorForState(new int[]{-android.R.attr.state_checked}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{android.R.attr.state_enabled}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_enabled}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{android.R.attr.state_window_focused}, stateList.getDefaultColor()),
stateList.getColorForState(new int[]{-android.R.attr.state_window_focused}, stateList.getDefaultColor()),
stateList.getDefaultColor()
}
但这只会涵盖孤立状态……您还可以组合状态,例如new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed, -android.R.attr.state_checked}
。 试图解释每个可能的状态是荒谬的,那么我怎么可能知道原始ColorStateList
设置了什么状态? 有没有更简单的方法来做到这一点? 是我想多了?
这会将 CheckedTextView 颜色从绿色更改为您指定的任何颜色
android:drawableTint="@color/grey_text"
它看起来像在CheckedTextView
着色是非常错误的。 最后,我通过交换onClickListener
颜色来解决它:
checkedTextView.setOnClickListener {
if (checkedTextView.isChecked) {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1)
} else {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2)
}
}
(例如在Kotlin,Java类似)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.