Jetpack 仅撰写粗体字符串占位符

Nic*_*nze 16 android android-jetpack-compose

我有一个像这样的字符串资源

<string name="my_string">Fancy string with an %1$s placeholder</string>
Run Code Online (Sandbox Code Playgroud)

我希望将其作为输出:“带有令人惊叹的占位符的精美字符串”。这是占位符内容以粗体显示的字符串。

我怎样才能得到想要的输出?

Nic*_*nze 32

最后我得到了想要的结果

val placeholder = "Amazing"

val globalText = stringResource(id = R.string.my_string, placeholder)

val start = globalText.indexOf(placeholder)
val spanStyles = listOf(
    AnnotatedString.Range(SpanStyle(fontWeight = FontWeight.Bold),
        start = start,
        end = start + placeholder.length
    )
)
Text(text = AnnotatedString(text = globalText, spanStyles = spanStyles))
Run Code Online (Sandbox Code Playgroud)


Rav*_*ahu 6

用这个

    @Composable
fun MultipleStylesInText() {
    Text(
        buildAnnotatedString {
            withStyle(style = SpanStyle(color = Color.Blue)) {
                append("H")
            }
            append("ello ")

            withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Red)) {
                append("W")
            }
            append("orld")
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

输出-输出图像

src - github /或/开发人员