如何在 Jetpack Compose 中将文本垂直对齐顶部、底部和中心? [英] How to align Text to Top, Bottom and Center Vertically in Jetpack Compose?

查看:393
本文介绍了如何在 Jetpack Compose 中将文本垂直对齐顶部、底部和中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Text 可组合函数垂直对齐文本.有没有办法做到这一点,而不必添加另一个额外的视图来包含 Text.

How can I align the text using Text composable function vertically. Is there a way to do it without having to add another extra view to contain the Text.

TexttextAlign 参数只有以下选项:

The textAlign parameter of Text only has the following options:

TextAlign.

  • 对了
  • 中心
  • 证明
  • 开始
  • 结束

我尝试过使用 textAlign = TextAlign.Center 但它只能水平居中.如何在不将其包裹在另一个视图中的情况下将其垂直居中?

I have tried using textAlign = TextAlign.Center but it only centers it horizontally. How can I center it vertically without wrapping it in another view?

Text(
    text = "Text",
    modifier = Modifier.size(100.dp),
    textAlign = TextAlign.Center
)

结果:

我想要达到的目标:

推荐答案

您必须使用父容器.

例如:

Box( 
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Center
) {
    Text(
        text = "Text",
    )
}

或:

Column(
    modifier = Modifier.fillMaxSize(),
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally
) {
    Text(
        text = "Text",
    )
}

这篇关于如何在 Jetpack Compose 中将文本垂直对齐顶部、底部和中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆