setSupportBackgroundTintList 状态不工作
setSupportBackgroundTintList states not working
我创建了一个扩展 AppCompat 按钮的 MyButton class。在我的讲师中,我执行了这段代码:
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{android.R.attr.state_pressed} // pressed
};
int[] colors = new int[]{
ContextCompat.getColor(context, R.color.tint),
ContextCompat.getColor(context, R.color.primary),
};
setSupportBackgroundTintList(new ColorStateList(states, colors));
不幸的是,状态不起作用。该按钮仅显示启用的颜色。我正在使用最新的 appcompat 库,也尝试了旧的
compile 'com.android.support:appcompat-v7:23.1.1' //also tried 23.0.1
compile 'com.android.support:design:23.1.1' //also tried 23.0.1
我做错了什么?
状态按照定义的顺序进行匹配。因此,android.R.attr.state_enabled
将在 之前匹配 android.R.attr.state_pressed
。
由于按钮已启用,第一个正匹配项将针对 android.R.attr.state_enabled
并且颜色 ContextCompat.getColor(context, R.color.tint)
将被选中。因为,已找到正匹配项,是否按下按钮并不重要。
解决这个问题的最快方法是将 android.R.attr.state_pressed
放在 android.R.attr.state_enabled
之前。状态匹配如下:
按钮当前被按下 --> 状态 android.R.attr.state_pressed
将被检查并找到正匹配 --> 颜色 ContextCompat.getColor(context, R.color.primary)
将被使用。
按钮当前 未 按下 --> 状态 android.R.attr.state_pressed
将被检查并且检查将失败 --> 状态 android.R.attr.state_enabled
将被检查并找到正匹配 --> 颜色 ContextCompat.getColor(context, R.color.tint)
将被使用。
这应该有效:
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}, // pressed
new int[]{android.R.attr.state_enabled} // enabled
};
int[] colors = new int[]{
ContextCompat.getColor(context, R.color.primary),
ContextCompat.getColor(context, R.color.tint)
};
setSupportBackgroundTintList(new ColorStateList(states, colors));
我创建了一个扩展 AppCompat 按钮的 MyButton class。在我的讲师中,我执行了这段代码:
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{android.R.attr.state_pressed} // pressed
};
int[] colors = new int[]{
ContextCompat.getColor(context, R.color.tint),
ContextCompat.getColor(context, R.color.primary),
};
setSupportBackgroundTintList(new ColorStateList(states, colors));
不幸的是,状态不起作用。该按钮仅显示启用的颜色。我正在使用最新的 appcompat 库,也尝试了旧的
compile 'com.android.support:appcompat-v7:23.1.1' //also tried 23.0.1
compile 'com.android.support:design:23.1.1' //also tried 23.0.1
我做错了什么?
状态按照定义的顺序进行匹配。因此,android.R.attr.state_enabled
将在 之前匹配 android.R.attr.state_pressed
。
由于按钮已启用,第一个正匹配项将针对 android.R.attr.state_enabled
并且颜色 ContextCompat.getColor(context, R.color.tint)
将被选中。因为,已找到正匹配项,是否按下按钮并不重要。
解决这个问题的最快方法是将 android.R.attr.state_pressed
放在 android.R.attr.state_enabled
之前。状态匹配如下:
按钮当前被按下 --> 状态
android.R.attr.state_pressed
将被检查并找到正匹配 --> 颜色ContextCompat.getColor(context, R.color.primary)
将被使用。按钮当前 未 按下 --> 状态
android.R.attr.state_pressed
将被检查并且检查将失败 --> 状态android.R.attr.state_enabled
将被检查并找到正匹配 --> 颜色ContextCompat.getColor(context, R.color.tint)
将被使用。
这应该有效:
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}, // pressed
new int[]{android.R.attr.state_enabled} // enabled
};
int[] colors = new int[]{
ContextCompat.getColor(context, R.color.primary),
ContextCompat.getColor(context, R.color.tint)
};
setSupportBackgroundTintList(new ColorStateList(states, colors));