【问题标题】:Is it possible to center text vertically是否可以垂直居中文本
【发布时间】:2020-10-05 20:16:53
【问题描述】:

是否可以从1.0.0-alpha03 开始垂直对齐文本中心而不嵌套组件Stack -> Text

 Text(
       modifier = Modifier
                 .size(150.dp, 30.dp)
                 .background(Colors.ChipGray, RoundedCornerShape(50)),
       textAlign = TextAlign.Center,
       text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
      )

【问题讨论】:

  • 是否要在Text() 组件内垂直对齐文本?

标签: android kotlin android-jetpack-compose


【解决方案1】:

一般来说,这是可能的,但我不建议这样做。

您可以使用偏移量将文本相对于背景移动一半字体大小,但我不确定您是否可以在此处访问所用字体的高度。

        Text(
            text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
            textAlign = TextAlign.Center,
            modifier = Modifier
                .size(150.dp, 30.dp)
                .background(Color.Gray, RoundedCornerShape(50))
                .offset(y = fontHeightHalf)
        )

改为使用堆栈,例如:

        Stack (
            alignment = Alignment.Center,
            modifier = Modifier
                .size(150.dp, 30.dp)
                .background(Color.Gray, RoundedCornerShape(50)),
        ) {
            Text(
                text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
                textAlign = TextAlign.Center
            )
        }

编辑:更新版本为 1.0.0-alpha03

【讨论】:

  • 非常感谢您的澄清。从 1.0.0-alpha03 开始​​,使用 Stack 有更好的方法。您可以直接在 Stack 上设置 align 参数。 android-review.googlesource.com/c/platform/frameworks/support/+/… BTW:重力修改器被对齐替换了
  • 这很好,让它更清晰一点。我更新了新版本的答案,我还在运行 1.0.0-alpha01。
猜你喜欢
  • 1970-01-01
  • 2013-10-15
  • 2015-09-16
  • 2015-06-06
  • 1970-01-01
  • 2023-03-05
  • 2014-03-02
相关资源
最近更新 更多