How to add button tint programmatically

前端 未结 15 968
时光取名叫无心
时光取名叫无心 2020-11-30 23:38

In the new AppCompat library, we can tint the button this way:

15条回答
  •  时光说笑
    2020-12-01 00:28

    In properly extending dimsuz's answer by providing a real code situation, see the following code snippet:

        Drawable buttonDrawable = button.getBackground();
        buttonDrawable = DrawableCompat.wrap(buttonDrawable);
        //the color is a direct color int and not a color resource
        DrawableCompat.setTint(buttonDrawable, Color.RED);
        button.setBackground(buttonDrawable);
    

    This solution is for the scenario where a drawable is used as the button's background. It works on pre-Lollipop devices as well.

提交回复
热议问题