简体   繁体   English

Jetpack Compose - 无法更改 Material 3 中的默认字体

[英]Jetpack Compose - Cannot change the default font in Material 3

In Material 2, the default font can be changed by overriding the defaultFontFamily property在 Material 2 中,可以通过覆盖defaultFontFamily属性来更改默认字体

Material 2 Compose材料 2 作曲

val Typography2 = Typography(
    defaultFontFamily = MyFont
)

But after upgrading to Material 3 it is not possible because there is no defaultFontFamily property但是升级到 Material 3 之后就不可能了,因为没有defaultFontFamily属性

Is there an alternative solution to apply the font throughout the App?是否有替代解决方案可以在整个应用程序中应用字体?

You can do it like this for example:例如,您可以这样做:

In Type.kt在 Type.kt 中

val HindSiliguri = FontFamily(
    Font(R.font.hindsiliguri_light, FontWeight.Light),
    Font(R.font.hindsiliguri_medium, FontWeight.Medium),
    Font(R.font.hindsiliguri_regular, FontWeight.Normal),
    Font(R.font.hindsiliguri_semibold, FontWeight.SemiBold),
    Font(R.font.hindsiliguri_bold, FontWeight.Bold),
)

val Typography = Typography(
    bodyMedium = TextStyle(
        fontFamily = HindSiliguri,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp
    )
)

In Theme.kt在 Theme.kt 中

@Composable
fun MyTheme(
    useDarkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable() () -> Unit
) {
    val colors = if (!useDarkTheme) {
        LightColors
    } else {
        DarkColors
    }

    MaterialTheme(
        colorScheme = colors,
        typography = Typography,
        content = content
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM