Android jetpack 在到达其容器时编写省略号换行内容文本

TRo*_*ose 1 android android-jetpack-compose

我目前正在使用 android jetpack compose,我想归档这样的行为。我有这样的用户界面在此输入图像描述。这里有一个 TextView(“名称”),wrap_content当它到达容器的末尾时,它会自动省略,如下所示在此输入图像描述

因此,为了在 XML 中存档此行为,我使用了

<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>
Run Code Online (Sandbox Code Playgroud)

我尝试在 jetpack compose 中执行相同的行为,但尚未找到方法。

我尝试用行来完成此操作,但第一个文本不会省略,直到它将第二个文本推出行并且其本身的宽度实际上超过了行的宽度

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)"
  )
}
Run Code Online (Sandbox Code Playgroud)

小智 7

使用width(IntrinsicSize.Max)代替fillMaxWidth()on Row 并赋予第一个文本权重。

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)"
    )
}
Run Code Online (Sandbox Code Playgroud)

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