How to add button tint programmatically

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

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

15条回答
  •  没有蜡笔的小新
    2020-12-01 00:40

    this is easily handled in the new Material Button from material design library, first, add the dependency:

    implementation 'com.google.android.material:material:1.1.0-alpha07'
    

    then in your XML, use this for your button:

    
    

    and when you want to change the color, here's the code in Kotlin, It's not deprecated and it can be used prior to Android 21:

    accept.backgroundTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, 
    R.color.colorPrimary, theme))
    

提交回复
热议问题