首页 > 解决方案 > 如何以编程方式将背景颜色从 android.support.design 更改为 MaterialButton

问题描述

我尝试使用以下代码将颜色更改为 MaterialButton:

var materialButton = findViewByid(R.id....) as MaterialButton
materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));

使用普通按钮,它可以工作。有人有想法么?

标签: androidkotlinmaterial-design

解决方案


我找到了如下答案:

我更改了以下行

materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));

materialButton.setBackgroundTintList(ContextCompat.getColorStateList(this@MyActivity, R.color.myCustomColor));

我以这种方式对其进行了测试,并且效果很好。


推荐阅读