简体   繁体   English

如何在 Android Compose 中仅省略中间文本?

[英]How ellipsis only middle text in Android Compose?

I am developing an android app using the jetpack compose.我正在使用 Jetpack Compose 开发一个 android 应用程序。

I want to make a UI like:我想制作一个像这样的用户界面:

在此处输入图像描述

Row(
    horizontalArrangement = Arrangement.SpaceBetween
) {
    Row(
        modifier = Modifier.weight(1F)
    ) {
        Text(
            text = name,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )

        Spacer(modifier = Modifier.width(4.dp))

        Text(
            text = "($count)",
            maxLines = 1,
        )
    }

    Spacer(modifier = Modifier.width(16.dp))

    Text(
        text = timeText,
    )
}

But actually it shows like:但实际上它显示如下:

在此处输入图像描述

the count information (1) is cut.计数信息(1)被截断。 How can I show the count information and ellipsis the name text?如何显示计数信息并省略名称文本?

Should I use the compose-constraintlayout?我应该使用 compose-constraintlayout 吗?

In such cases Modifier.weight should be used on the view, in this case it'll be measured after other siblings:在这种情况下,应该在视图上使用Modifier.weight ,在这种情况下,它将在其他兄弟姐妹之后进行测量:

The parent will divide the vertical space remaining after measuring unweighted child elements and distribute it according to this weight. parent会划分测量未加权的子元素后剩余的垂直空间,并按照这个权重进行分配。

If you don't need the view to take all the space available, add fill = false parameter.如果不需要视图占用所有可用空间,请添加fill = false参数。

Text(
    text = name,
    maxLines = 1,
    overflow = TextOverflow.Ellipsis,
    modifier = Modifier.weight(1f, fill = false)
)

Spacer(modifier = Modifier.width(4.dp))

Text(
    text = "($count)",
    maxLines = 1,
)

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

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