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

Mar*_*ius 26 android android-constraintlayout

ConstraintLayout只要在他的右边有另一个视图的空间,是否有可能让视图增长?

用例是除了彼此之外还有一个valueunitTextViews.该valueTextView的应该可以,只要有空间的成长unit.如果没有足够的空间,value应该切割.

ConstraintLayout

我用链子和其他一些东西尝试过但是无法完成它.在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" />
Run Code Online (Sandbox Code Playgroud)

Pav*_*van 39

是的,您可以使用match_constraint(0dp)等于match_parent用于其他布局,因此通过使用match_constraint我们为第一个视图设置权​​重,这将占用所有可用空间也添加

app:layout_constraintWidth_default="wrap"
Run Code Online (Sandbox Code Playgroud)

将默认宽度行为应用为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>
Run Code Online (Sandbox Code Playgroud)

从网站得到一些解释

更好地查看尺寸控制

维度设置为0dp (MATCH_CONSTRAINT)时的新可用行为.和以前一样,两个端点(左/右或上/下)需要连接到目标.

layout_constraintWidth_default = spread(默认,类似于上一行为)layout_constraintWidth_default = wrap layout_constraintHeight_default = spread layout_constraintHeight_default = wrap

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

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