android - 如何以编程方式将背景颜色从 android.support.design 更改为 MaterialButton
问题描述
我尝试使用以下代码将颜色更改为 MaterialButton:
var materialButton = findViewByid(R.id....) as MaterialButton
materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));
使用普通按钮,它可以工作。有人有想法么?
解决方案
我找到了如下答案:
我更改了以下行
materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));
到
materialButton.setBackgroundTintList(ContextCompat.getColorStateList(this@MyActivity, R.color.myCustomColor));
我以这种方式对其进行了测试,并且效果很好。
推荐阅读
- json - 无法将 JSON 数据发布到 GitHub API(可能还有其他)
- .net-core - 阻止 EF Core 使用合并插入
- sql - 通过 case 语句和标志进行 SQL 分区
- sql - 理解/优化 Postgresql 中的 SQL 查询
- html - 导航栏与标题重叠,框阴影不显示,无法更改 h1 字体大小
- javascript - 如何仅从Javascript中的完整路径中提取文件名+扩展名?
- ssh - 如何通过 ssh 连接到服务器上的特定计算机?
- ios - SwiftUI View 入门代码在添加到现有项目时抱怨“从非协议类型 'View' 继承”
- ipfs - 同一文件的 CID 是否不同但由 IPFS 中的 2 个用户上传?
- c - Visual Studio Code:参数未传递给 C 命令行应用程序