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

Compose自定义字体添加失败:Android Studio提示找不到h1参数

解决Jetpack Compose Material3中Typography无法识别h1参数的问题

错误原因

你使用的是Material3的Typography构造器,它的参数命名规则和Material2完全不同,不存在h1这个参数。Material3重新定义了文本样式的层级命名,比如原Material2的h1对应Material3中的headlineLarge(常规标题层级)或displayLarge(大尺寸展示类标题),具体可根据设计需求选择。

解决方法

方法1:替换为Material3官方参数名

直接将代码中的h1替换为Material3对应的标准参数,示例如下:

val Typography = Typography(
    bodyLarge = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp,
        lineHeight = 24.sp,
        letterSpacing = 0.5.sp
    ),
    headlineLarge = TextStyle( // 替换h1为headlineLarge
        color = TextWhite,
        fontFamily = gothicA1,
        fontWeight = FontWeight.Bold,
        fontSize = 22.sp
    )
)

Material3与Material2的文本样式对应关系参考:

  • Material2 h1 → Material3 headlineLarge
  • Material2 h2 → Material3 headlineMedium
  • Material2 h3 → Material3 headlineSmall
  • 更大的展示类标题可使用displayLarge/displayMedium/displaySmall

方法2:自定义扩展Typography(保留h1命名)

如果希望继续使用h1作为样式名,可以通过扩展属性实现:

// 定义扩展属性
val Typography.h1: TextStyle
    get() = headlineLarge.copy(
        color = TextWhite,
        fontFamily = gothicA1,
        fontWeight = FontWeight.Bold,
        fontSize = 22.sp
    )

// 原Typography基础配置保持不变
val Typography = Typography(
    bodyLarge = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp,
        lineHeight = 24.sp,
        letterSpacing = 0.5.sp
    )
)

使用时直接调用:

Text(
    text = "示例文本",
    style = MaterialTheme.typography.h1
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:10:21