[英]How to Change The Checked Tint Color of a CheckedTextView with Hex Value
I want to dynamically change the tint color of a CheckedTextView
when the state of the view is checked
.我想在checked
视图状态时动态更改CheckedTextView
的色调颜色。 I am pretty sure I can achieve this by calling setCheckMarkTintList
on the CheckedTextView
.我很确定我可以通过在CheckedTextView
上调用setCheckMarkTintList
来实现这CheckedTextView
。 To do this, I need a ColorStateList
, but the problem is I want to retain all of the colors for each state of the CheckedTextView
, except for the checked
state.为此,我需要一个ColorStateList
,但问题是我想保留CheckedTextView
每个状态的所有颜色,除了checked
状态。
So, I can obtain the ColorStateList
of the CheckedTextView
, but I do not know of a way to change only the color for the checked
state.因此,我可以获得ColorStateList
的CheckedTextView
,但我不知道如何仅更改checked
状态的颜色。 I know I can create a new ColorStateList
, but how do I make sure it retains all of the values from the original?我知道我可以创建一个新的ColorStateList
,但是如何确保它保留原始值的所有值?
I can create a state list like this:我可以创建这样的状态列表:
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
}
And create a color list from the colors from the original ColorStateList
:并从原始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()
}
But this would only cover the solitary states... you can also combine states, such as new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed, -android.R.attr.state_checked}
.但这只会涵盖孤立状态……您还可以组合状态,例如new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed, -android.R.attr.state_checked}
。 It would be ridiculous to try to account for every possible state, so how can I possibly know what states the original ColorStateList
has set?试图解释每个可能的状态是荒谬的,那么我怎么可能知道原始ColorStateList
设置了什么状态? Is there an easier way of doing this?有没有更简单的方法来做到这一点? Am I overthinking it?是我想多了?
This changes the CheckedTextView color from green to whatever color you specify这会将 CheckedTextView 颜色从绿色更改为您指定的任何颜色
android:drawableTint="@color/grey_text" android:drawableTint="@color/grey_text"
it looks like tinting in a CheckedTextView
is pretty buggy. 它看起来像在CheckedTextView
着色是非常错误的。 At the end I solved it by swapping colors in onClickListener
: 最后,我通过交换onClickListener
颜色来解决它:
checkedTextView.setOnClickListener {
if (checkedTextView.isChecked) {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1)
} else {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2)
}
}
(example in Kotlin, Java is similar) (例如在Kotlin,Java类似)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.