我正在尝试更改可点击文本的样式,主要是其字体和颜色。这是我的代码:
ClickableText(
text = AnnotatedString(stringResource(R.string.forgot_password)),
onClick = { offset ->
Log.d("ClickableText", "$offset -th character is clicked.")
}
)
这只是使用默认主题。如何应用不同的颜色或字体?
它提供了
style
参数。你可以做类似的事情
ClickableText(
text = AnnotatedString(""),
onClick = {},
style = TextStyle(
color = Blue,
fontSize = 26.sp,
fontFamily = FontFamily.Cursive
)
)
如果您使用的是 Android Studio,则只需在 Windows 上按
Ctrl + P
和 Mac 上按 Cmd + P
即可查看可用参数。可选参数不会通过代码完成插入,因为它们可能很多。
要使用字体,请定义一个
fontFamily
属性,如下所示
private val Montserrat = FontFamily(
Font(R.font.montserrat_regular, FontWeight.Normal),
Font(R.font.montserrat_medium, FontWeight.Medium),
Font(R.font.montserrat_bold, FontWeight.Bold),
)
然后将其添加到您的版式中
val Typography = Typography(
h1 = TextStyle(
fontFamily = Montserrat,
fontSize = 96.sp,
fontWeight = FontWeight.Normal,
lineHeight = 117.sp,
letterSpacing = (-1.5).sp
),
已添加到您的主题中
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
如果您在文本中使用这些样式,它将选择您指定的系列,或者您可以覆盖,就像这样
Text(
text = "Sample text",
style = MaterialTheme.typography.body1.copy(
color = Color.Blue,
fontFamily = Montserrat,
),
)
可以使用style关键字来添加字体,例如:
ClickableText(
text = AnnotatedString(text = "Clickable Text Font Example"),
style = TextStyle(
fontFamily = FontFamily(Font(R.font.your_font)),
),
onClick = {}
)