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

Jetpack Compose中Text组件出现红线报错问题求助

Jetpack Compose Material3 Text组件报错解决方法

你遇到的红线问题根源有两个:

  1. Material3的Text组件无backgroundColor参数:该参数是旧版Material2 Text的API,你当前依赖Material3库,方法签名不匹配导致报错。
  2. Greeting函数缺少@Composable注解:Compose组件(如Column、Text)和stringResource只能在Composable上下文中使用,普通函数无法调用这些API。

修复步骤

  1. 给Greeting函数添加@Composable注解,标识这是一个可组合函数。
  2. 将Text的backgroundColor参数替换为Modifier.background(),把背景色设置移到Modifier链中(这是Compose推荐的样式统一设置方式)。

修复后完整代码

@Composable
fun Greeting(one: String, two: String, three: String, four: String, modifier: Modifier = Modifier) {
    Column(Modifier.fillMaxWidth()) {
        Row(Modifier.weight(1f)) {
            Column(
                modifier = modifier
                    .fillMaxSize()
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = one,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(bottom = 16.dp)
                        .weight(1f)
                        .background(Color.Green)
                )
                Text(
                    text = two,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .weight(1f)
                        .background(Color.Yellow)
                )
            }
        }
        Row(Modifier.weight(1f)) {
            Column(
                modifier = modifier
                    .fillMaxSize()
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = three,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .weight(1f)
                        .padding(bottom = 16.dp)
                        .background(Color.Cyan)
                )
                Text(
                    text = four,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .weight(1f)
                        .background(Color.LightGray)
                )
            }
        }
    }
}

// 必须在Composable函数中调用Greeting
@Composable
fun ExampleScreen() {
    Greeting(
        stringResource(id = R.string.one),
        stringResource(id = R.string.two),
        stringResource(id = R.string.three),
        stringResource(id = R.string.four)
    )
}

额外检查

  • 确认项目依赖的是Material3库,避免Material2和Material3 API混用:
    // build.gradle.kts 或 build.gradle 中的依赖配置
    implementation("androidx.compose.material3:material3:1.2.0") // 替换为你使用的版本
    
  • 所有Composable函数必须添加@Composable注解,且只能在其他Composable函数内调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 03:15:28