Refer to : How do I create ColorStateList programmatically?
https://stackoverflow.com/questions/15543186/how-do-i-create-colorstatelist-programmatically?answertab=active#tab-top
一、基本使用
xml 代码与 java 代码对比
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/white"/>
<item android:color="@color/black"/>
</selector>
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{}//default
},
new int[] {
context.getResources().getColor(R.color.white),
context.getResources().getColor(R.color.black)
}
);
使用示例 1:
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_selected};//selected state
states[1] = new int[]{};//default state
int[] colors = new int[]{
Color.parseColor(bottomBar.activeColor), //selected state color
Color.parseColor(bottomBar.inActiveColor)//default state color
};
ColorStateList colorStateList = new ColorStateList(states, colors);
使用示例 2:
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
};
int[] colors = new int[] {
Color.BLACK,
Color.RED,
Color.GREEN,
Color.BLUE
};
ColorStateList myList = new ColorStateList(states, colors);
二、扩展使用
创建自定义 ColorStateListBuilder 类
private class ColorStateListBuilder {
List<Integer> colors = new ArrayList<>();
List<int[]> states = new ArrayList<>();
public ColorStateListBuilder addState(int[] state, int color) {
states.add(state);
colors.add(color);
return this;
}
public ColorStateList build() {
return new ColorStateList(convertToTwoDimensionalIntArray(states),
convertToIntArray(colors));
}
private int[][] convertToTwoDimensionalIntArray(List<int[]> integers) {
int[][] result = new int[integers.size()][1];
Iterator<int[]> iterator = integers.iterator();
for (int i = 0; iterator.hasNext(); i++) {
result[i] = iterator.next();
}
return result;
}
private int[] convertToIntArray(List<Integer> integers) {
int[] result = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; iterator.hasNext(); i++) {
result[i] = iterator.next();
}
return result;
}
}
使用 ColorStateListBuilder 类
ColorStateListBuilder builder = new ColorStateListBuilder();
builder.addState(new int[] { android.R.attr.state_pressed }, ContextCompat.getColor(this, colorRes))
.addState(new int[] { android.R.attr.state_selected }, Color.GREEN)
.addState(..., some color);
if(// some condition){
builder.addState(..., some color);
}
builder.addState(new int[] {}, colorNormal); // must add default state at last of all state
ColorStateList stateList = builder.build(); // ColorStateList created here
// textView.setTextColor(stateList);
三、所有状态类型参考
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>

本文详细介绍了如何在Android中通过XML和Java创建ColorStateList,包括基本用法示例、扩展的ColorStateListBuilder类,并罗列了所有状态类型的参考。适合开发者掌握颜色状态选择器的动态设置技巧。
449

被折叠的 条评论
为什么被折叠?



