You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:52:06