【发布时间】:2021-11-13 20:04:55
【问题描述】:
我有两个横向约束在一起的长文本。由于两个文本都可能很长,因此都用省略号截断。这是供参考的图表。
| [文本视图 1] [文本视图 2] |
假设我们为TextViews 显示相同的长文本:"Hello there I am a very very long text"。我需要做的是,如果没有足够的空间容纳TextViews,那么先截断右边的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..] |
这是我在发布之前检查过的一些帖子。
ConstraintLayout prioritizing textview truncation
Expand TextView with wrap_content until the neighbor view reaches the end of the parent
【问题讨论】:
标签: android android-layout android-constraintlayout