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

Kotlin Compose编译报错:找不到modifier和contentScale参数怎么办?

解决Compose编译错误:找不到modifier和contentScale参数

你的问题出在参数位置错误:painterResource 仅负责加载Drawable资源,它的参数只有id,并不支持modifier和contentScale。这两个参数是Image可组合组件的配置项,不属于资源加载函数。

修正后的代码如下:

@Composable
fun BirthdayGreetingWithImage(message: String, from: String) {
    val imagePainter = painterResource(id = R.drawable.androidparty)
    Image(
        painter = imagePainter,
        contentDescription = "Birthday party image", // 必须添加,用于无障碍支持
        modifier = Modifier.fillMaxHeight().fillMaxWidth(),
        contentScale = ContentScale.Crop
    )
}

额外提醒:Image组件要求必须设置contentDescription,用于屏幕阅读器等无障碍场景,请勿省略。

内容的提问来源于stack exchange,提问作者kariku_k

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:05:18