How to add button tint programmatically

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

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

相关标签:
15条回答
  • 2020-12-01 00:41

    here's how to do it in kotlin:

    view.background.setTint(ContextCompat.getColor(context, textColor))
    
    0 讨论(0)
  • 2020-12-01 00:41

    I had a similar problem. I wished to colour a complex drawable background for a view based on a color (int) value. I succeeded by using the code:

    ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{color});
    textView.setBackgroundTintList(csl);
    

    Where color is an int value representing the colour required. This represents the simple xml ColorStateList:

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:color="color here"/>
    </selector>
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 00:42

    The way I managed to get mine to work was by using CompoundButtonCompat.setButtonTintList(button, colour).

    To my understanding this works regardless of android version.

    0 讨论(0)
提交回复
热议问题