【问题标题】:How to prioritize TextView truncation如何优先考虑 TextView 截断
【发布时间】: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


    【解决方案1】:

    对于左侧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>
    

    【讨论】:

    • 感谢它为我工作。我正在使用“ app:layout_constraintWidth_default="wrap"" 这给了我一点类似的结果,但是左侧的TextView 与右侧的TextView 重叠。另外,显然 layout_constraintWidth_default 已弃用?再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多