我想CheckedTextView
在视图状态为时动态更改 a 的色调颜色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
设置了什么?有没有更简单的方法来做到这一点?我是不是想多了?