【问题标题】:Alignment/wrapping inside the ConstraintLayoutConstraintLayout 内的对齐/换行
【发布时间】:2020-06-17 01:35:23
【问题描述】:

我有 3 个水平放置的文本视图。最左边的一个和最右边的一个是固定大小的,但是中间的一个长度可以有很大的不同。像这样的:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <TextView
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingEnd="8dp"
            android:text="LEFT"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingEnd="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/right"
            app:layout_constraintStart_toEndOf="@+id/left"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@tools:sample/lorem/random" />

        <TextView
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RIGHT"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/message"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这会产生以下布局:

但是如果我们中间有短文本,像这样:

tools:text="@tools:sample/lorem"

它会看起来:

最右边的视图“粘”到最后。

我希望它跟随中间视图,像这样:

如何实现?

【问题讨论】:

标签: android android-constraintlayout


【解决方案1】:

您应该将app:layout_constraintWidth_default="wrap" 用于中间视图并使用app:layout_constraintHorizontal_chainStyle="packed"。另外不要忘记为第一个视图设置app:layout_constraintHorizontal_bias="0.0",以将整个布局与屏幕的开头对齐。下面是整个布局的外观:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="LEFT"
        app:layout_constraintEnd_toStartOf="@id/message"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="8dp"
        app:layout_constraintEnd_toStartOf="@id/apply_text"
        app:layout_constraintStart_toEndOf="@id/left"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap"
        tools:text="@tools:sample/lorem" />

    <TextView
        android:id="@+id/apply_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="RIGHT"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/message"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果如下:

【讨论】:

    【解决方案2】:

    这是一个相当大的挑战。 AFAIK,要做你想做的事,你必须将中间 TextView 的宽度更改为 wrap_content 并使用父权限删除右 TextView 约束。

    您可以尝试计算左右宽度,然后如果设备的总宽度大于或等于 left + right + middle 然后您从

    到这里

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 1970-01-01
      • 2018-10-13
      • 2019-09-30
      • 2017-12-03
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      相关资源
      最近更新 更多