Jetpack Compose Row 中的等宽元素

发布于 2025-01-14 05:45:38 字数 156 浏览 3 评论 0原文

在 Jetpack Compose 中使 Row 的所有子项的宽度相同的最简单方法是什么?例如,如果 Row 中有 4 个 Box 元素,我希望每个 Box 的宽度为 1/4(减去填充)整行的。

What is the easiest way to make the width the same for all the children of a Row in Jetpack Compose? For example, if there are 4 Box elements within a Row, I want each Box to have 1/4 the width (minus the padding) of the entire row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晒暮凉 2025-01-21 05:45:38

您可以使用修饰符。包含最大宽度并向框添加重量的行

Modifier.fillMaxWidth().weight(1f)

,例如,

Row(
    modifier = Modifier
        .fillMaxWidth()
        .padding(5.dp)
) {
    for (i in 0..3) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(100.dp)
                .weight(1f)
                .padding(horizontal = 5.dp)
                .background(Color.Blue)
        )
    }
}

You can use modifier. Row which contains max width and add weight to the box like

Modifier.fillMaxWidth().weight(1f)

For example,

Row(
    modifier = Modifier
        .fillMaxWidth()
        .padding(5.dp)
) {
    for (i in 0..3) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(100.dp)
                .weight(1f)
                .padding(horizontal = 5.dp)
                .background(Color.Blue)
        )
    }
}
海的爱人是光 2025-01-21 05:45:38

在行上使用 spacedBy 属性,在子项上使用 weight 修饰符。查看下面的示例:

@Preview(showBackground = true)
@Composable
fun EquiRow() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        repeat(4) {
            Box(
                modifier = Modifier
                    .weight(1f)
                    .height(96.dp)
                    .background(Color.Blue)
            ) {

            }
        }
    }
}

Use spacedBy attribute on Row and weight modifier on children. Checkout the below example:

@Preview(showBackground = true)
@Composable
fun EquiRow() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        repeat(4) {
            Box(
                modifier = Modifier
                    .weight(1f)
                    .height(96.dp)
                    .background(Color.Blue)
            ) {

            }
        }
    }
}
ㄖ落Θ余辉 2025-01-21 05:45:38

对所有元素使用 weight 修饰符。Modifier.weight(1)

use the weight modifier.. Modifier.weight(1) on all the elements

离去的眼神 2025-01-21 05:45:38

您可以对该行的子行使用 Modifier.weight(1f)

一个很好的例子是这 3 个不同长度的文本。

 Row(Modifier.padding(4.dp)) {
    
            Text(
                text = "boy",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
            Spacer(modifier = Modifier.width(4.dp))
            Text(
                text = "girls",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
    
            Spacer(modifier = Modifier.width(4.dp))
            Text(
                text = "countries",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
...}

我们只需应用 Modifier.weight (1F) 即可使宽度相同,尽管字符数量不同。

You can use Modifier.weight(1f) on the row's children.

A good example is these 3 Texts with different lengths.

 Row(Modifier.padding(4.dp)) {
    
            Text(
                text = "boy",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
            Spacer(modifier = Modifier.width(4.dp))
            Text(
                text = "girls",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
    
            Spacer(modifier = Modifier.width(4.dp))
            Text(
                text = "countries",
                modifier = Modifier
                        .weight(1f)
                        .border(width = 1.dp, Color.Blue, shape = RectangleShape)
                        .padding(4.dp)
    
            )
...}

We simply apply Modifier.weight (1F) to make the width the same despite the different number of characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文