当视图的状态为CheckedTextView时,我想动态地更改checked的色调。我确信我可以通过在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设置了哪些状态呢?有更简单的方法吗?我是不是想得太多了?
发布于 2021-08-14 11:44:00
这会将CheckedTextView颜色从绿色更改为指定的任何颜色。
Android:drawableTint=“@color/灰色文本”
发布于 2016-10-23 16:36:13
它看起来像在一个CheckedTextView的着色是相当的小马车。最后,我通过在onClickListener中交换颜色来解决这个问题
checkedTextView.setOnClickListener {
if (checkedTextView.isChecked) {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1)
} else {
checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2)
}
}(例如在Kotlin,Java类似)
发布于 2022-10-06 07:46:30
如果需要以编程方式更改它,可以设置如下所示的ColorStateList:
int[] colors = new int[] {
color,
color,
color,
color
};
int[][] states = new int[][] {
new int[] { android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
ColorStateList myList = new ColorStateList(states, colors);
checkedTextView.setCheckMarkTintList(myList);https://stackoverflow.com/questions/36482003
复制相似问题