【发布时间】:2020-08-01 13:27:00
【问题描述】:
我有一个 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的值被挤出了屏幕。
所以我尝试了嵌套线性布局,如下所示:
https://stackoverflow.com/a/55251997/7764977
使用以下代码:
<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 中。所以我不能得到一个标题来匹配最长标题的宽度。
【问题讨论】:
标签: android android-layout android-linearlayout android-constraintlayout