How can I modify ripple color when using ?attr/selectableItemBackground as background?

前端 未结 8 1289
情深已故
情深已故 2020-11-28 18:22

I\'ve seen some SO questions and they gave some possible methods to achieve what I want. For example:

  1. Use colorControlHighlight attribute in st

8条回答
  •  萌比男神i
    2020-11-28 18:53

    This code works for me to create a ripple:

    public static void setRippleDrawable(View view, int normalColor, int touchColor) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                RippleDrawable rippleDrawable = new RippleDrawable(ColorStateList.valueOf(touchColor), view.getBackground(), null);
                view.setBackground(rippleDrawable);
            } else {
                StateListDrawable stateListDrawable = new StateListDrawable();
                stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(touchColor));
                stateListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(touchColor));
                stateListDrawable.addState(new int[]{}, new ColorDrawable(normalColor));
                view.setBackground(stateListDrawable);
                }
        } catch (Exception e) {
            Log.e(LOG_TAG, "" + e);
        }
    }
    

    I did not found any way to modify the selectableItemBackground attribute. That's why I did it like above.

提交回复
热议问题