Заполнить пространство и многоточие текста - PullRequest
2 голосов
/ 30 апреля 2019

У меня есть макет, состоящий из 2 текстовых представлений, выровненных по горизонтали. Правое текстовое представление должно всегда отображать весь текст, а левое должно расширяться, чтобы занимать все необходимое пространство, с многоточием, если оно не помещается. Когда оба вида соответствуют друг другу, они должны занимать только минимальное пространство.

Эти изображения иллюстрируют то, что я ищу:

Когда подходит весь текст:

enter image description here

Когда левый текст слишком длинный:

enter image description here

У меня есть этот начальный макет, но я не могу заставить его работать для обоих сценариев одновременно.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:ellipsize="end"
        android:lines="1"
        android:textSize="24sp"
        app:layout_constraintHorizontal_chainStyle="packed"
        android:text="Left text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Right text"
        android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/left"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

1 Ответ

2 голосов
/ 01 мая 2019

Это может быть достигнуто путем создания упакованной цепочки со смещением 0 для выравнивания по началу и установки app:layout_constrainedWidth="true" для обоих TextViews, чтобы их ограничения учитывались при переносе содержимого.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:ellipsize="end"
            android:lines="1"
            android:textSize="24sp"
            android:text="Left text"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@id/right"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:text="Right teasdasda asdsdsddddddd"
            android:textSize="24sp"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/left"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
...