Android Button Drawable Tint

前端 未结 7 1636
自闭症患者
自闭症患者 2020-12-04 18:13

Is it possible to tint the drawableLeft in an android button? I have a black drawable I\'d like to tint white. I know how to achieve this with an image view (image on the le

7条回答
  •  孤城傲影
    2020-12-04 18:31

    I know there are lots of answers already, but non of them made me happy since I didn't want to have different drawables for different styles of elements.

    So my solution was to set color filter in constructor like this:

    int textColor = getTextColors().getColorForState(EMPTY_STATE_SET, Color.WHITE);
        Drawable[] drawables = getCompoundDrawablesRelative();
        for (Drawable drawable : drawables) {
            if(drawable != null) {
                drawable.setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
            }
        }
    

    I used text color because this was what I need, but it can be replaced with custom attribute to be more dinamic.

提交回复
热议问题