Android SelectableItem更改棒棒糖前版本的背景色
对于棒棒糖,可以使用Android SelectableItem更改棒棒糖前版本的背景色,android,ripple,Android,Ripple,对于棒棒糖,可以使用colorControlHighlight简单地修改波纹颜色。但是对于预棒棒糖(=21。如果需要支持旧版本,只需使用backgroundTint属性(不带android前缀)。我已经用API 15在设备上进行了测试。谢谢,更新了答案 <item name="colorControlHighlight">@color/yourColor</item>
colorControlHighlight
简单地修改波纹颜色。但是对于预棒棒糖(?attr/selectableItemBackground
编辑来更改按下状态的颜色:现在可以使用AppCompat和
以前的解决方案:
不确定这是否是您想要的,但我最终以编程方式完成了这项工作:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList colors = new ColorStateList(new int[][]{
new int[]{android.R.attr.state_enabled},
}, new int[]{pressed});
GradientDrawable item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
RippleDrawable ripple = new RippleDrawable(colors, item, null);
button.setBackgroundDrawable(ripple);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable item;
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(pressed);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, item);
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, item);
button.setBackgroundDrawable(stateListDrawable);
}
您可以简单地使用:
<item name="colorControlHighlight">@color/yourColor</item>
@color/yourColor
您可以使用选择器android:backgroundTint
需要API>=21。如果需要支持旧版本,只需使用backgroundTint
属性(不带android
前缀)。我已经用API 15在设备上进行了测试。谢谢,更新了答案
<item name="colorControlHighlight">@color/yourColor</item>