Android Jetpack Compose文本在达到其容器时自动换行并显示省略号

huangapple go评论65阅读模式
英文:

Android jetpack compose ellipsis wrap content Text when it reaches its container

问题

在Jetpack Compose中实现相同的行为可能需要使用ConstraintLayout来控制子元素的布局。以下是一个示例代码:

ConstraintLayout(
    modifier = Modifier.fillMaxWidth()
) {
    val (nameRef, countRef) = createGuidelineFromTopToBottom(0.5f)
    
    Text(
        modifier = Modifier
            .constrainAs(nameRef) {
                top.linkTo(parent.top)
                start.linkTo(parent.start)
                end.linkTo(countRef)
            }
            .background(Color.Cyan),
        text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )

    Text(
        modifier = Modifier
            .constrainAs(countRef) {
                top.linkTo(parent.top)
                bottom.linkTo(parent.bottom)
                end.linkTo(parent.end)
            }
            .background(Color.Red),
        text = "(0)"
    )
}

请注意,这只是一个示例代码,实际的布局可能会根据您的需求和UI进行调整。同时,您可能需要调整createGuidelineFromTopToBottom中的参数以获得所需的布局效果。

英文:

I'm currently working with android jetpack compose and i want to archive a behavior like this. I have UI like this Android Jetpack Compose文本在达到其容器时自动换行并显示省略号. Here a TextView ("a name") is wrap_content and when it reaches the end of the container it'll automatically be ellipsised like this Android Jetpack Compose文本在达到其容器时自动换行并显示省略号.

So here in order to archive this behavior in XML I used

<TableLayout
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:shrinkColumns="0"
  app:layout_constraintEnd_toStartOf="@id/icon_container"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toBottomOf="@id/card">
  <TableRow android:layout_height="match_parent">
    <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ellipsize="end"
      android:maxLines="1"
      android:textAlignment="textStart"
      tools:text="a name a name a name a name a name" />

    <TextView
      android:id="@+id/count"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:text="(213)" />
  </TableRow>
</TableLayout>

I try to do the same behavior in jetpack compose but haven't found out a way to do it.

I try to do it with a Row but the first Text is not ellipsised until it push the second Text out of the Row and the width of itself is actually exceed the Row's width

Row(modifier = Modifier.fillMaxWidth()) {
  Text(
    modifier = Modifier
      .background(Color.Cyan),
    text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
    maxLines = 1,
    overflow = TextOverflow.Ellipsis
  )
  Text(
    modifier = Modifier
      .background(Color.Red),
    text = "(0)"
  )
}

答案1

得分: 6

使用width(IntrinsicSize.Max)替代fillMaxWidth()Row上,并为第一个文本添加权重。

Row(modifier = Modifier.width(IntrinsicSize.Max)) {
    Text(
        modifier = Modifier.weight(1f)
            .background(Color.Cyan),
        text = "周末 周末 周末 周末 周末 周末 ",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
    Text(
        modifier = Modifier
            .background(Color.Red),
        text = "(0)"
    )
}

Row将尝试扩展其宽度以适应两个文本,直到达到最大宽度。在Row内,第二个文本被放置,第一个文本占据其余的空间。

英文:

Use width(IntrinsicSize.Max) instead of fillMaxWidth() on Row and give weight to the first text.

Row(modifier = Modifier.width(IntrinsicSize.Max)) {
    Text(
        modifier = Modifier.weight(1f)
            .background(Color.Cyan),
        text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
    Text(
        modifier = Modifier
            .background(Color.Red),
        text = "(0)"
    )
}

Row will try to expand its width to the size that can fit both texts until it reaches the max width.
Inside of Row, the second text is placed and the first take the rest of the space.

答案2

得分: 2

使用 Modifier.weight(),将 'fill' 参数设置为 false。

Row {
    Text(
        modifier = Modifier
            .weight(1f, fill = false)
            .background(Color.Cyan),
        text = "周末 周末 周末 周末 周末 周末 ",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
    Text(
        modifier = Modifier.background(Color.Red),
        text = "(0)"
    )
}
英文:

Use Modifier.weight() with 'fill' parameter as false.

    Row {
        Text(
            modifier = Modifier
                .weight(1f, fill = false)
                .background(Color.Cyan),
            text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )
        Text(
            modifier = Modifier.background(Color.Red),
            text = "(0)"
        )
    }

huangapple
  • 本文由 发表于 2023年7月13日 17:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定