我正在尝试使用ConstraintLayout
使以下看似简单但棘手的布局。
标题较长:
我尝试了以下代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="April Ludgate"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/threadType"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap" />
<TextView
android:id="@+id/threadType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="SMS"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:layout_constraintEnd_toStartOf="@+id/dummy"
app:layout_constraintStart_toEndOf="@+id/title"
app:layout_constraintTop_toTopOf="@+id/title"
app:layout_constraintWidth_default="wrap" />
<View
android:id="@+id/dummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:layout_constraintEnd_toStartOf="@+id/date"
app:layout_constraintStart_toEndOf="@+id/threadType"
app:layout_constraintTop_toTopOf="@+id/title"
app:layout_constraintWidth_default="spread" />
<TextView
android:id="@+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="4:18 PM"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dummy"
app:layout_constraintTop_toTopOf="@+id/title"
app:layout_constraintWidth_default="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>
哪个给我这个:
但是很快就消失了,并使用了更长的名称:
是否只有通过xml才能通过ConstraintLayout
实现此目标?如果愿意,我愿意切换到另一种布局。
将maxlength设置为textview.it可能会解决您的问题
在title
上的ID为android:layout_width="wrap_content"
android:layout_width="0dp"
的TextView中替换