使用ContraintLayout的简单但棘手的布局

问题描述 投票:1回答:2

我正在尝试使用ConstraintLayout使以下看似简单但棘手的布局。

普通用户界面:enter image description here

标题较长:

enter image description here

我尝试了以下代码:

<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>

哪个给我这个:

enter image description here

但是很快就消失了,并使用了更长的名称:

enter image description here

是否只有通过xml才能通过ConstraintLayout实现此目标?如果愿意,我愿意切换到另一种布局。

android android-layout android-constraintlayout
2个回答
0
投票

将maxlength设置为textview.it可能会解决您的问题


0
投票

title上的ID为android:layout_width="wrap_content" android:layout_width="0dp"的TextView中替换

© www.soinside.com 2019 - 2024. All rights reserved.