简体   繁体   English

如何在 Jetpack Compose 中对齐来自不同行的两个文本

[英]How to align two texts from different rows in Jetpack Compose

I have 3 texts in one row and then another three in another row inside a LazyColumn我在一行中有 3 个文本,然后在 LazyColumn 内的另一行中有另外三个

图片

I want to align the "text" in the center of the first row with "15.025" value in the second row我想将第一行中心的“文本”与第二行的“15.025”值对齐

I've used我用过

TextAlign.Center

on both of the Texts and在文本和

             horizontalArrangement = Arrangement.SpaceBetween,
             verticalAlignment = Alignment.CenterVertically

set this on the Row but it doesn't work.将其设置在 Row 上,但它不起作用。 Any tips please?请问有什么提示吗?

I'm pretty new in Compose, sorry for the basic question.我是 Compose 的新手,很抱歉这个基本问题。

You can use Modifier.weight to make the width of "left view" equals to the width of "right view".您可以使用Modifier.weight使“左视图”的宽度等于“右视图”的宽度。 It will make the middle text on each row always center.它将使每一行的中间文本始终居中。

Row(Modifier.fillMaxWidth()) {
    Text(
        text = "Start",
        modifier = Modifier.weight(1f)
    )
    Text(
        text = "Center",
        textAlign = TextAlign.Center, // you can also fixed the width of center text
    )
    Text(
        text = "End",
        modifier = Modifier.weight(1f),
        textAlign = TextAlign.End
    )
}

If your text content is always short, you can also use Box to make the middle text always center.如果您的文本内容总是很短,您还可以使用Box使中间文本始终居中。

Box(Modifier.fillMaxWidth()) {
    Text(text = "Start", modifier = Modifier.align(Alignment.CenterStart))
    Text(text = "Center", modifier = Modifier.align(Alignment.Center))
    Text(text = "End", modifier = Modifier.align(Alignment.CenterEnd))
}

This is what I wanted to achieve and with the help of @Linh I did这就是我想要实现的目标,在@Linh 的帮助下我做到了

在此处输入图像描述

Here's how I did it.这就是我的做法。

Column(Modifier.fillMaxSize()) {
    Row(Modifier.fillMaxWidth()) {
        Text(
            text = "Start",
            modifier = Modifier.weight(1.5f),
            textAlign = TextAlign.Start
        )
        Text(
            text = "Center",
            modifier = Modifier.weight(1f),
            textAlign = TextAlign.Start
        )
        Text(
            text = "End",
            modifier = Modifier.weight(1f),
            textAlign = TextAlign.End
        )
    }
}

Thanks again for the help.再次感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM