我试图用编程方式创建一个ColorStateList:
ColorStateList stateList = new ColorStateList(states, colors);
但我不确定这两个参数是什么。
根据文件:
public ColorStateList (int[][] states, int[] colors) 在API级别1中添加 创建一个ColorStateList,返回从状态到颜色的指定映射。
谁能给我解释一下怎么做这个?
状态的二维数组是什么意思?
我试图用编程方式创建一个ColorStateList:
ColorStateList stateList = new ColorStateList(states, colors);
但我不确定这两个参数是什么。
根据文件:
public ColorStateList (int[][] states, int[] colors) 在API级别1中添加 创建一个ColorStateList,返回从状态到颜色的指定映射。
谁能给我解释一下怎么做这个?
状态的二维数组是什么意思?
当前回答
不幸的是,没有一个解决方案适合我。
如果你一开始不设置按下状态,它就不会检测到。 如果你设置了它,那么你需要定义空状态来添加默认颜色
ColorStateList themeColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_focused, android.R.attr.state_pressed},
new int[]{-android.R.attr.state_enabled},
new int[]{} // this should be empty to make default color as we want
},
new int[]{
pressedFontColor,
defaultFontColor,
pressedFontColor,
disabledFontColor,
defaultFontColor
}
);
这是来自源代码的构造函数:
/**
* Creates a ColorStateList that returns the specified mapping from
* states to colors.
*/
public ColorStateList(int[][] states, int[] colors) {
mStateSpecs = states;
mColors = colors;
if (states.length > 0) {
mDefaultColor = colors[0];
for (int i = 0; i < states.length; i++) {
if (states[i].length == 0) {
mDefaultColor = colors[i];
}
}
}
}
其他回答
我的构建器类创建ColorStateList
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 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);
有时候这就足够了:
int colorInt = getResources().getColor(R.color.ColorVerificaLunes);
ColorStateList csl = ColorStateList.valueOf(colorInt);
科特林:
val colorInt: Int = context.getColor(R.color.ColorVerificaLunes)
val csl = ColorStateList.valueOf(colorInt)
如果你使用Colors.xml资源
int[] colors = new int[] {
getResources().getColor(R.color.ColorVerificaLunes),
getResources().getColor(R.color.ColorVerificaMartes),
getResources().getColor(R.color.ColorVerificaMiercoles),
getResources().getColor(R.color.ColorVerificaJueves),
getResources().getColor(R.color.ColorVerificaViernes)
};
ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{colors[0]});
example.setBackgroundTintList(csl);
下面是如何在Kotlin中以编程方式创建ColorList的示例:
val colorList = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_enabled), // Disabled
intArrayOf(android.R.attr.state_enabled) // Enabled
),
intArrayOf(
Color.BLACK, // The color for the Disabled state
Color.RED // The color for the Enabled state
)
)
不幸的是,没有一个解决方案适合我。
如果你一开始不设置按下状态,它就不会检测到。 如果你设置了它,那么你需要定义空状态来添加默认颜色
ColorStateList themeColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_focused, android.R.attr.state_pressed},
new int[]{-android.R.attr.state_enabled},
new int[]{} // this should be empty to make default color as we want
},
new int[]{
pressedFontColor,
defaultFontColor,
pressedFontColor,
disabledFontColor,
defaultFontColor
}
);
这是来自源代码的构造函数:
/**
* Creates a ColorStateList that returns the specified mapping from
* states to colors.
*/
public ColorStateList(int[][] states, int[] colors) {
mStateSpecs = states;
mColors = colors;
if (states.length > 0) {
mDefaultColor = colors[0];
for (int i = 0; i < states.length; i++) {
if (states[i].length == 0) {
mDefaultColor = colors[i];
}
}
}
}