使用ConstraintLayout进行简单但棘手的布局 [英] Simple yet tricky layout using ConstraintLayout

查看:44
本文介绍了使用ConstraintLayout进行简单但棘手的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make the following seemingly simple yet tricky layout using 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中实现?如果愿意,我愿意切换到另一种布局.

Is there a way to achieve this via ConstraintLayout just in xml? I am willing to switch to another layout if it accomplished what I want.

推荐答案

使用constraintHorizontal_chainStyle title 中的包装constraintHorizontal_bias一起 0.0 如下:

Use constraintHorizontal_chainStyle to packed in title with constraintHorizontal_bias to 0.0 like below:

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

输出:

  • 短文本

  • 长文本

这篇关于使用ConstraintLayout进行简单但棘手的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆