У меня есть макет CardView , который я передаю в RecyclerView . Есть шесть (6) TextView с, с двумя (2) TextView с на строку. TextView слева - это заголовки, а справа - значения, взятые из базы данных.
Я хочу, чтобы ширина заголовков соответствовала ширине самого длинного заголовка. и значения заполняют оставшееся пространство внутри CardView следующим образом:
В этом примере Тема - самое длинное слово, поэтому остальные заголовки соответствуют ширине темы
Я попытался использовать 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. Поэтому я не могу получить один заголовок, соответствующий ширине самого длинного заголовка.