【问题标题】:Simple yet tricky layout using ConstraintLayout使用 ConstraintLayout 的简单但复杂的布局
【发布时间】:2020-02-11 06:39:07
【问题描述】:

我正在尝试使用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: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>

给我这个:

但很快就会因为名字变长而分崩离析:

有没有办法通过ConstraintLayout 仅在xml 中实现这一点?如果它完成了我想要的,我愿意切换到另一个布局。

【问题讨论】:

  • 为什么不以编程方式尝试字符计数并在超出限制时添加这些点?
  • 你应该试试线性布局
  • 这并不棘手,只是一个糟糕的设计。如果 SMS 是一个标签,那么为什么它会随文本浮动?我认为,用户倾向于在同一个地方查看标签,如果标签是浮动的,那么用户必须查看整行才能确定它是短信。
  • @AshwiniViolet 这不是关于 UX 或图形设计的问题。我有一个想要在 Android 中实现的布局。
  • @M-WaJeEh 这就是为什么它只是一个评论,而不是一个答案,我不认为它有什么不好的

标签: android android-layout android-constraintlayout


【解决方案1】:

使用constraintHorizontal_chainStyle packedtitle 中,constraintHorizontal_bias0.0 如下所示:

<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="April Ludgate April Ludgate April Ludgate April Ludgate"
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintHorizontal_chainStyle="packed"
        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_marginRight="4dp"
        android:text="SMS"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@+id/title"
        app:layout_constraintEnd_toStartOf="@+id/date"
        app:layout_constraintStart_toEndOf="@+id/title"
        app:layout_constraintTop_toTopOf="@+id/title"
        app:layout_constraintWidth_default="wrap" />

    <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_constraintTop_toTopOf="@+id/title"
        app:layout_constraintWidth_default="wrap" />

</androidx.constraintlayout.widget.ConstraintLayout>

输出:

  • 短文本

  • 长文本

【讨论】:

  • 哇太棒了...甚至没有考虑使用打包链类型。
猜你喜欢
  • 2019-05-01
  • 2019-03-24
  • 2020-09-20
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
相关资源
最近更新 更多