如何以编程方式将背景颜色从android.support.design更改为MaterialButton

如何以编程方式将背景颜色从android.support.design更改为MaterialButton

问题描述:

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

I try change color to MaterialButton with this code:

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

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

With a normal Button, it works. Anyone have any ideas?

我找到了如下答案:

我更改了以下行

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

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

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

I tested it that way and it worked perfectly.