我试图用编程方式创建一个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,返回从状态到颜色的指定映射。
谁能给我解释一下怎么做这个?
状态的二维数组是什么意思?
当前回答
根据Jonathan Ellis的回答,在Kotlin中,你可以定义一个帮助函数,使代码更习惯,更容易阅读,所以你可以这样写:
val colorList = colorStateListOf(
intArrayOf(-android.R.attr.state_enabled) to Color.BLACK,
intArrayOf(android.R.attr.state_enabled) to Color.RED,
)
colorStateListOf可以像这样实现:
fun colorStateListOf(vararg mapping: Pair<IntArray, Int>): ColorStateList {
val (states, colors) = mapping.unzip()
return ColorStateList(states.toTypedArray(), colors.toIntArray())
}
我还有:
fun colorStateListOf(@ColorInt color: Int): ColorStateList {
return ColorStateList.valueOf(color)
}
这样我就可以调用相同的函数名,不管它是选择器还是单一颜色。
其他回答
第一个维度是状态集的数组, 第二个是状态集本身。 colors数组列出了每个匹配状态集的颜色,因此colors数组的长度必须与states数组的第一个维度相匹配(否则在“使用”状态时将崩溃)。 这里有例子:
ColorStateList myColorStateList = new ColorStateList(
new int[][] {
new int[] {
android.R.attr.state_pressed
}, //1
new int[] {
android.R.attr.state_focused
}, //2
new int[] {
android.R.attr.state_focused, android.R.attr.state_pressed
} //3
},
new int[] {
Color.RED, //1
Color.GREEN, //2
Color.BLUE //3
}
);
编辑的例子: 一个XML颜色状态列表,如:
<?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[]{}
},
new int[] {
context.getResources().getColor(R.color.white),
context.getResources().getColor(R.color.black)
}
);
不幸的是,没有一个解决方案适合我。
如果你一开始不设置按下状态,它就不会检测到。 如果你设置了它,那么你需要定义空状态来添加默认颜色
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];
}
}
}
}
如果你使用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);
根据Jonathan Ellis的回答,在Kotlin中,你可以定义一个帮助函数,使代码更习惯,更容易阅读,所以你可以这样写:
val colorList = colorStateListOf(
intArrayOf(-android.R.attr.state_enabled) to Color.BLACK,
intArrayOf(android.R.attr.state_enabled) to Color.RED,
)
colorStateListOf可以像这样实现:
fun colorStateListOf(vararg mapping: Pair<IntArray, Int>): ColorStateList {
val (states, colors) = mapping.unzip()
return ColorStateList(states.toTypedArray(), colors.toIntArray())
}
我还有:
fun colorStateListOf(@ColorInt color: Int): ColorStateList {
return ColorStateList.valueOf(color)
}
这样我就可以调用相同的函数名,不管它是选择器还是单一颜色。
有时候这就足够了:
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)