如何在 Kotlin 中以编程方式更改 cardview 背景颜色

How to change cardview background color Programmatically in Kotlin

我尝试这样做:

cardview.cardBackgroundColor(Color)

但我收到此消息:未找到函数 'invoke()'。

有什么想法吗?

您尝试过使用

cardView.setCardBackgroundColor(Color.RED);

或从如下参考中创建任何颜色

 val color = ContextCompat.getColor(this@SplashActivity, R.color.colorPrimary)
 cardView.setCardBackgroundColor(color);

找到了,忘了set

cardview.setCardBackgroundColor(Color)

现在一切正常,我的 cardview 背景改变了。