using a custom color for button background while using selectableItemBackground attribute

后端 未结 6 2295
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 21:37

I am trying to use

android:background=\"?android:attr/selectableItemBackground\"

to get my button to do appropriate effects for each android versi

6条回答
  •  一个人的身影
    2021-02-06 22:07

    Edit: this is now possible using AppCompat and backgroundTint

       android:backgroundTint="@color/yourColor"
    

    Previous solution:

    I had the same problem and ended up doing this programmatically:

        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);
        }
    

提交回复
热议问题