我有一个非常简单的
Jetpack-Compose
可组合函数:
@Preview(showBackground = true)
@Composable
fun QuoteScreen() {
QuoteAppTheme {
Text(
"Hello World",
fontFamily = cursiveFontFamily
)
}
}
Text
具有来自Google Fonts的自定义字体,该字体已在Type.kt
中定义为:
val provider = GoogleFont.Provider(
providerAuthority = "com.google.android.gms.fonts",
providerPackage = "com.google.android.gms",
certificates = R.array.com_google_android_gms_fonts_certs
)
val cursiveFontFamily = FontFamily(
Font(
googleFont = GoogleFont("Shadows Into Light Two"),
fontProvider = provider
)
)
当我运行应用程序或在模拟器中预览时,字体会正确应用
但是在Android Studio预览中,字体没有应用。我们能以某种方式解决这个问题吗?
使用 GoogleFonts 库时遇到类似的问题。手动下载并导入字体文件似乎可以解决我的问题。
一步一步:
下载字体并将其重命名,删除所有大写字母和破折号,例如
shadowsintolighttwo_regular.ttf
更换
val provider = GoogleFont.Provider(
providerAuthority = "com.google.android.gms.fonts",
providerPackage = "com.google.android.gms",
certificates = R.array.com_google_android_gms_fonts_certs
)
val cursiveFontFamily = FontFamily(
Font(
googleFont = GoogleFont("Shadows Into Light Two"),
fontProvider = provider
)
)
与
val cursiveFontFamily = FontFamily(
Font(
R.font.shadowsintolighttwo_regular, FontWeight.Regular
)
)