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

How ellipsis only middle text in Android Compose?

我正在使用 Jetpack Compose 开发 android 应用程序。

我想制作一个 UI 像:

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,
    )
}

但实际显示如下:

计数信息(1)被截断。 如何显示计数信息并省略名称文本?

我应该使用 compose-constraintlayout 吗?

在这种情况下,应在视图上使用 Modifier.weight,在这种情况下,它将在其他兄弟姐妹之后进行测量:

The parent will divide the vertical space remaining after measuring unweighted child elements and distribute it according to this weight.

如果您不需要视图获取所有可用的 space,请添加 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,
)