Jetpack Compose按钮背景色设置报错:Cannot find a parameter with this name: backgroundColor
解决Jetpack Compose按钮背景色设置报错问题
你遇到的错误是因为Jetpack Compose版本迭代后,ButtonDefaults.buttonColors()的参数名发生了变化:新版本中不再使用backgroundColor,而是改用containerColor来指定按钮的背景色。
修正后的代码如下:
Button( onClick = { /*TODO*/ }, colors = ButtonDefaults.buttonColors(containerColor = Color.Yellow) ) { Text(text = "Button") }
补充说明:如果你的项目仍在使用Compose 1.0.x系列版本,backgroundColor参数是有效的;但从Compose 1.1版本开始,官方统一用containerColor控制按钮容器背景,contentColor控制按钮内部文字、图标等内容的颜色,建议跟进版本规范使用新参数名。
内容的提问来源于stack exchange,提问作者Jorpy
相关产品推荐
相关产品推荐

