Jetpack Compose:SpanStyle的文本对齐方式(垂直居中)

5

在使用 SpanStyle 时如何实现 Gravity.CENTER_VERTICAL

不要使用其他容器组件,如BoxRow等...

val spanString = buildAnnotatedString {
    withStyle(
        SpanStyle(
            color = Color(0xFF333333),
            fontSize = 32.sp
        )
    ) {
        append("Jetpack")
    }
    withStyle(
        SpanStyle(
            color = Color(0xFF999999),
            fontSize = 14.sp,
            fontWeight = FontWeight.Bold
        )
    ) {
        append(" Compose")
    }
}
Text(
    modifier = Modifier.background(color = Color.White),
    text = spanString
)

现在:
Img

期望:
Img

1个回答

3
将此添加到你的第二个 SpanStyle 中。
 baselineShift = BaselineShift.Superscript

修改后的代码

 val spanString = buildAnnotatedString {
        withStyle(
            SpanStyle(
                color = Color(0xFF333333),
                fontSize = 32.sp
            )
        ) {
            append("Jetpack")
        }
        withStyle(
            SpanStyle(
                baselineShift = BaselineShift.Superscript, // added line
                color = Color(0xFF999999),
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold
            )
        ) {
            append(" Compose")
        }
    }
    Text(
        modifier = Modifier.background(color = Color.White),
        text = spanString,
        textAlign = TextAlign.Center
    )

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接