如何确定 TextView 截断的优先级
How to prioritize TextView truncation
我有两个水平约束在一起的长文本。由于两个文本都可能很长,因此都用省略号截断。这是供参考的图表。
| [文本视图 1] [文本视图 2] |
假设我们要为两个 TextView
显示相同的长文本:"Hello there I am a very very long text"
。我需要做的是,如果没有足够的 space 来容纳两个 TextView
,那么先截断右边的 TextView,然后截断左边的 TextView。
这是我想要的结果:
| [Hello there I am a very very long text] [Hello...] |
这是我到目前为止尝试过的方法:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:padding="20dp">
<TextView
android:id="@+id/leftTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#333333"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rightTv"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello there I am a very very very very long text"
tools:text="Hello there I am a very very very very long text" />
<TextView
android:id="@+id/rightTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:textColor="#21b38a"
android:textSize="16sp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftTv"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello there I am a very very very very long text"
tools:text="Hello there I am a very very very very long text" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的代码输出如下:
| [Hello there I am a very..] [Hello there I am a very..] |
这是我发帖前检查过的一些帖子。
Expand TextView with wrap_content until the neighbor view reaches the end of the parent
对于左侧 TextView,指定 app:layout_constrainedWidth="true"
宽度为 wrap_content
。对于右边的 TextView,指定 app:layout_constraintWidth_min="50dp"
和 wrap_content
的宽度。 (如果不指定最小宽度,当左视图足够大时,视图将缩小为空。)
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/leftTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Hello this is some very, very, very, very, very, very, very, very, very long text."
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rightTv"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/rightTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Hello this is some very, very, very, very, very, very long text."
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftTv"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_min="50dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我有两个水平约束在一起的长文本。由于两个文本都可能很长,因此都用省略号截断。这是供参考的图表。
| [文本视图 1] [文本视图 2] |
假设我们要为两个 TextView
显示相同的长文本:"Hello there I am a very very long text"
。我需要做的是,如果没有足够的 space 来容纳两个 TextView
,那么先截断右边的 TextView,然后截断左边的 TextView。
这是我想要的结果:
| [Hello there I am a very very long text] [Hello...] |
这是我到目前为止尝试过的方法:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:padding="20dp">
<TextView
android:id="@+id/leftTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#333333"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rightTv"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello there I am a very very very very long text"
tools:text="Hello there I am a very very very very long text" />
<TextView
android:id="@+id/rightTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:textColor="#21b38a"
android:textSize="16sp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftTv"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello there I am a very very very very long text"
tools:text="Hello there I am a very very very very long text" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的代码输出如下:
| [Hello there I am a very..] [Hello there I am a very..] |
这是我发帖前检查过的一些帖子。
Expand TextView with wrap_content until the neighbor view reaches the end of the parent
对于左侧 TextView,指定 app:layout_constrainedWidth="true"
宽度为 wrap_content
。对于右边的 TextView,指定 app:layout_constraintWidth_min="50dp"
和 wrap_content
的宽度。 (如果不指定最小宽度,当左视图足够大时,视图将缩小为空。)
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/leftTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Hello this is some very, very, very, very, very, very, very, very, very long text."
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rightTv"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/rightTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Hello this is some very, very, very, very, very, very long text."
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftTv"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_min="50dp" />
</androidx.constraintlayout.widget.ConstraintLayout>