通过另一个视图限制 ConstraintLayout 中的宽度

Restrict width in ConstraintLayout by another view

是否有可能(在 ConstraintLayout 中)让视图仅在他右边有 space 另一个视图时才增长?

用例是让 valueunit TextView 彼此相邻。只要 unit 有 space,value TextView 就应该能够增长。如果space不够用,就把value剪掉。

我已经用链条和其他一些东西试过了,但无法完成。 value 不会停止增长,然后 unit 就不再可见了。这是当前代码:

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:lines="1"
    app:layout_constraintBaseline_toBaselineOf="@+id/unit"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/unit"
    tools:text="12533939532" />

<TextView
    android:id="@+id/unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintLeft_toRightOf="@id/value"
    app:layout_constraintRight_toRightOf="parent"
    tools:text="km" />

是的,您可以使用 match_constraint (0dp),对于其他布局,它等于 match_parent,因此使用 match_constraint 我们为第一个视图设置权​​重,它将占据所有可用的 space 还添加

app:layout_constraintWidth_default="wrap"

将默认宽度行为应用为 wrap_content

这里是有变化的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

        <TextView
            android:id="@+id/value"
            android:layout_width="0dp"
            app:layout_constraintWidth_default="wrap"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:lines="1"
            app:layout_constraintBaseline_toBaselineOf="@+id/unit"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/unit"
            tools:text="12533939532" />

        <TextView
            android:id="@+id/unit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintLeft_toRightOf="@id/value"
            app:layout_constraintRight_toRightOf="parent"
            tools:text="km" />

    </android.support.constraint.ConstraintLayout>

从站点得到一些解释

更好的视图维度控件

维度设置为 0dp 时的新可用行为 (MATCH_CONSTRAINT)。和以前一样,两个端点(left/right 或 top/bottom)都需要连接到目标。

layout_constraintWidth_default = spread(默认,类似于之前的行为) layout_constraintWidth_default = 换行 layout_constraintHeight_default = 传播 layout_constraintHeight_default = 换行

Wrap 提供了一个重要的新行为,小部件调整大小就像使用 wrap_content 一样,但受连接约束的限制。因此,小部件不会超出端点。

http://tools.android.com/recent/constraintlayoutbeta5isnowavailable