pre-lollipop版本的selectableItemBackground颜色变化

Kam*_*mal 5 android ripple

对于Lollipop,可以简单地使用修改波纹颜色colorControlHighlight.但对于pre-Lollipop(<API21),如何通过将背景设置为更改所实现的按下状态的颜色?attr/selectableItemBackground

mbo*_*nin 1

编辑:现在可以使用AppCompat和backgroundTint来实现

   backgroundTint="@color/yourColor"
Run Code Online (Sandbox Code Playgroud)

以前的解决方案:

不确定这是否是您想要的,但我最终以编程方式执行此操作:

    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);
    }
Run Code Online (Sandbox Code Playgroud)