如何以编程方式将背景颜色从android.support.design更改为MaterialButton [英] How to change background Color to MaterialButton from android.support.design programmatically

查看:86
本文介绍了如何以编程方式将背景颜色从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.

这篇关于如何以编程方式将背景颜色从android.support.design更改为MaterialButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆