是否可以垂直居中文本
Is it possible to center text vertically
从 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(
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 -> 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(
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