ConstraintLayout内部的对齐/包装

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

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

这将导致以下布局:

very long message

但是如果中间有短文字,例如:

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

它将看起来:

short message

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

我希望它遵循中间视图,例如:

desired layout

如何实现?

android android-constraintlayout
2个回答
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>

这里是结果:

““>

“完整”


0
投票

这是相当大的挑战。 AFAIK,要执行所需的操作,必须将中间TextView的宽度更改为wrap_content,并删除具有父级权限的TextView约束。

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