从Jetpack Compose 1.2.0开始,AnnotatedStringBuilder.withStyle不再支持直接调用stringResource函数。如果您需要在构建样式的同时访问Resource字符串,请使用LocalContext.current.getString,将Resource ID作为参数传递。
以下是一个示例代码:
val annotatedString = AnnotatedStringBuilder().apply {
withStyle(
style = SpanStyle(color = Color.Red)
) {
append(LocalContext.current.getString(R.string.my_string_resource))
}
}
上面代码中,R.string.my_string_resource是一个字符串资源ID,使用LocalContext.current.getString将resource ID作为参数传递,从而解决了AnnotatedStringBuilder.withStyle不能调用stringResource的问题。