【发布时间】:2015-08-07 09:32:12
【问题描述】:
对于 Lollipop,可以使用 colorControlHighlight 简单地修改波纹颜色。但是对于pre-Lollipop(?attr/selectableItemBackground来更改按下状态的颜色@
【问题讨论】:
-
你可以使用选择器
对于 Lollipop,可以使用 colorControlHighlight 简单地修改波纹颜色。但是对于pre-Lollipop(?attr/selectableItemBackground来更改按下状态的颜色@
【问题讨论】:
你可以简单地使用这个:
<item name="colorControlHighlight">@color/yourColor</item>
【讨论】:
编辑:现在可以使用 AppCompat 和 backgroundTint
backgroundTint="@color/yourColor"
以前的解决方案:
不确定这是您想要的,但我最终以编程方式执行此操作:
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);
}
【讨论】:
android:backgroundTint 需要 API >=21。如果您需要支持旧版本,只需使用 backgroundTint 属性(不带 android 前缀)。我已经在使用 API 15 的设备上对此进行了测试。