简体   繁体   English

如何让元素填充 Jetpack Compose 中行或列中的剩余空间

[英]How to make element fill the remaining space inside a Row or a Column in Jetpack Compose

I am trying to show two text messages besides each other in a row but when the size of the first text is big, the second view gets pushed out of the screen as shown below:我试图在一行中并排显示两条短信,但是当第一个文本的大小很大时,第二个视图被推出屏幕,如下所示:

The code:代码:

Row(modifier = Modifier.fillMaxWidth()) {
    Text(
        text = "safasfasdfsasdssdsaasdsadsdsaasdsasdsasdasddassdsssdasdadsasdasdsd",
        modifier = Modifier
            .padding(top = 12.dp, bottom = 12.dp, end = 12.dp, start = 10.dp)
            .background(Color.Gray)
    )
    Text(
        text = "12:00 am",
        style = messageTimeTextStyle,
        modifier = Modifier
            .padding(horizontal = 16.dp),
        maxLines = 1
    )
}

The output:输出: 在此处输入图像描述

You can apply the weight modifier only to the long text.只能weight修饰符应用于长文本。

The .weight modifier sizes the element's width proportional to its weight relative to other weighted sibling elements in the Row . .weight修饰符调整元素的宽度,使其与Row中其他加权兄弟元素的权重成正比。 The parent will divide the horizontal space remaining after measuring unweighted child elements and distribute it according to this weight父级将测量未加权的子元素后剩余的水平空间划分,并根据此权重分配

Something like:就像是:

Row() {
    Text(text = "safasfasdfsasdssdsaasdsadsdsaasdsasdsasdasddassdsssdasdadsasdasdsd",
        Modifier
            .padding(top = 12.dp, bottom = 12.dp, end = 12.dp, start = 10.dp)
            .background(Color.Gray)
            .weight(1f)
         )
    Text(
        text = "12:00 am",
        modifier = Modifier
            .padding(horizontal = 16.dp),
        maxLines = 1
    )
}

在此处输入图像描述

or或者

Row() {
    Column(Modifier.weight(1f)){
        Text(text = "safasfasdfsasdssdsaasdsadsdsa.." , ....)
    }
    Column() {
        Text( text = "12.00 ..", .....)
    }
}

If you wrap your text in boxes and give your boxes weight, then you get what you are looking for:如果您将文本包装在盒子中并赋予盒子重量,那么您会得到您正在寻找的东西:

Row(modifier = Modifier.fillMaxWidth()) {
        Box(Modifier.weight(2f)) {
            Text(
                text = "safasfasdfsasdssdsaasdsafasfasdfsasdssdsaasdsafasfasdfsasdssdsaasd",
                Modifier
                    .padding(top = 12.dp, bottom = 12.dp, end = 12.dp, start = 10.dp)
                    .background(Color.Gray)
            )
        }
        Box(Modifier.weight(1f)) {
            Text(
                text = "12:00 am",
                modifier = Modifier
                    .padding(horizontal = 16.dp),
                maxLines = 1
            )
        }
    }

If you assign weight only to the Text, it will take all the space available and it will leave just enough space that is needed for the Box.如果您只为 Text 分配权重,它将占用所有可用空间,而它只会留下 Box 所需的足够空间。 I am copying and editing the same code from the reply above by @Code Poet.我正在复制和编辑@Code Poet 上面回复中的相同代码。

    Row(modifier = Modifier.fillMaxWidth()) {
        Box(Modifier.weight(2f)) {
            Text(
                text = "safasfasdfsasdssdsaasdsafasfasdfsasdssdsaasdsafasfasdfsasdssdsaasd",
                Modifier
                    .padding(top = 12.dp, bottom = 12.dp, end = 12.dp, start = 10.dp)
                    .background(Color.Gray)
            )
        }
        Box {
            Text(
                text = "12:00 am",
                maxLines = 1
            )
        }
    }
}

Preview:预习: 在此处输入图像描述

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

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