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

Android项目构建失败:Unresolved reference: fontSize问题求助

解决Compose中Text fontSize "Unresolved reference"错误
  • 检查自定义Composable的参数定义
    如果你是在调用GreetingText时写了fontSize = 80.sp,但GreetingText本身没声明这个参数,就会报这个错。比如错误写法:

    @Composable
    fun GreetingText(name: String) {
        Text(text = "Hello $name!")
    }
    
    // 调用时错误传递fontSize
    GreetingText(name = "Android", fontSize = 80.sp)
    

    修正方式:给GreetingText添加fontSize参数,并传递给内部的Text组件:

    @Composable
    fun GreetingText(name: String, fontSize: TextUnit) {
        Text(
            text = "Hello $name!",
            fontSize = fontSize
        )
    }
    
    // 正常调用
    GreetingText(name = "Android", fontSize = 80.sp)
    
  • 检查Text组件的参数拼写与位置
    如果是在GreetingText内部的Text里写fontSize = 80.sp出错,先确认拼写(比如是不是写成了小写s的fontsize),再检查参数是否写在Text的括号内,没有放错代码块。

  • 确认Compose依赖版本
    旧版本的Compose可能存在参数名差异,确保你的Module级build.gradle里用的是稳定版依赖,比如:

    dependencies {
        implementation "androidx.compose.ui:ui:1.5.4"
        implementation "androidx.compose.material3:material3:1.1.2"
    }
    
  • 检查Text组件的导入
    确保你导入的是Compose官方的Text组件,而非其他库的,同时确认sp的导入正确:

    import androidx.compose.material3.Text
    import androidx.compose.ui.unit.sp
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 08:54:56