Android -Layout: поместите два TextView в ряд и отрегулируйте размер второго TextView на основе размера первого - PullRequest
0 голосов
/ 19 апреля 2020

У меня есть макет CardView , который я передаю в RecyclerView . Есть шесть (6) TextView с, с двумя (2) TextView с на строку. TextView слева - это заголовки, а справа - значения, взятые из базы данных.

Я хочу, чтобы ширина заголовков соответствовала ширине самого длинного заголовка. и значения заполняют оставшееся пространство внутри CardView следующим образом:

enter image description here

В этом примере Тема - самое длинное слово, поэтому остальные заголовки соответствуют ширине темы

Я попытался использовать ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    app:cardCornerRadius="4dp"
    android:elevation="8dp"
    android:focusable="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical"
        android:paddingStart="8dp"
        android:paddingTop="8dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp">

        <TextView
            android:id="@+id/to"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/to"
            android:textColor="#000000"
            android:textSize="16sp"
            app:layout_constraintTop_toBottomOf="@id/date"/>

        <TextView
            android:id="@+id/from"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/from"
            android:textColor="#000000"
            android:textSize="16sp"
            app:layout_constraintTop_toBottomOf="@id/to"/>

        <TextView
            android:id="@+id/subjectLabelTextView"
            android:layout_width="96dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/subject"
            android:textColor="#000000"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/subject"
            app:layout_constraintTop_toBottomOf="@id/from" />

        <TextView
            android:id="@+id/subject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yo, it's summertime. It's rainy though mate. Let's hangout at Macy's later."
            android:textColor="#000000"
            android:textSize="18sp"
            app:layout_constraintLeft_toRightOf="@id/subjectLabelTextView"
            app:layout_constraintTop_toTopOf="@id/subjectLabelTextView" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Проблема в том, что когда текст слишком длинный, значение правого TextView выталкивается из экрана.

Итак, я попробовал вложенную линейную компоновку, как показано здесь:

{ ссылка }

со следующим кодом:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test test test test test test test test test test test test test test test"
                android:layout_weight="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello world"
                android:layout_weight="0"/>
    </LinearLayout>

    <Space android:layout_width="wrap_content" android:layout_height="20dp"/>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test test test test test test test test "
                android:layout_weight="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello world"
                android:layout_weight="0"/>
    </LinearLayout>

</LinearLayout>

Это почти идеально. Тем не менее, каждая строка размещается в разных LinearLayout. Поэтому я не могу получить один заголовок, соответствующий ширине самого длинного заголовка.

1 Ответ

1 голос
/ 19 апреля 2020

Вы можете использовать Barrier для такой договоренности. Таким образом, все 3 заголовка TextViews будут началом ограничений Barrier, а все 3 содержимого TextViews будут концом ограничения Barrier. Таким образом, любой из оставленных TextView увеличится в ширину и будет sh контент TextViews ближе к концу. Попробуйте следующий макет: -

 <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        app:cardCornerRadius="4dp"
        android:elevation="8dp"
        android:focusable="true">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:paddingStart="8dp"
            android:paddingTop="8dp"
            android:paddingEnd="8dp"
            android:paddingBottom="8dp">

        <TextView
                android:id="@+id/to"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="To"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        <TextView
                android:id="@+id/from"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="From"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/toContent" />

        <TextView
                android:id="@+id/subjectLabelTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Subject"
                android:textColor="#000000"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fromContent" />

        <androidx.constraintlayout.widget.Barrier
                android:id="@+id/barrier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:barrierDirection="end"
                app:constraint_referenced_ids="to,subjectLabelTextView,from"
                tools:layout_editor_absoluteX="395dp"
                tools:layout_editor_absoluteY="8dp" />

        <TextView
                android:id="@+id/subject"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Yo, it's summertime. It's rainy though mate. Let's hangout at Macy's later."
                android:textColor="#000000"
                android:layout_marginStart="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:textSize="18sp"
                app:layout_constraintStart_toEndOf="@id/barrier"
                app:layout_constraintTop_toTopOf="@id/subjectLabelTextView" />


        <TextView
                android:id="@+id/toContent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Me ertime. It's rainy though mate. Let's hangouMacy'"
                android:textColor="#000000"
                android:textSize="18sp"
                android:layout_marginStart="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/barrier"
                app:layout_constraintTop_toTopOf="@id/to" />

        <TextView
                android:id="@+id/fromContent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="it's me"
                android:textColor="#000000"
                android:textSize="18sp"
                android:layout_marginStart="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/barrier"
                app:layout_constraintTop_toTopOf="@id/from" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
...