Limit width in ConstraintLayout to another view

Is there a possibility (in ConstraintLayout ) that the view is displayed only until there is no space left for another view to the right of it?

A use case should consist of value and unit TextViews, apart from each other. value TextView should be able to grow as long as there is room for unit . If there is not enough space, you should disable value .

Constraintlayout

I tried this with chains and some other things, but can't do it. value does not stop growing, and then unit no longer displayed. Here's the current code:

 <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" /> 
+7
android android-constraintlayout
source share
1 answer

yes, you can use match_constraint (0dp), which is equal to match_parent for another layout, so using match_constraint we set the weight for the first view, which will occupy all the free space also add

 app:layout_constraintWidth_default="wrap" 

to apply default width behavior like wrap_content

here is the code with the change

 <?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="12533939zx532" /> <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> 

got some explanation from the site

Larger Size Controls

New available behavior when size is 0dp (MATCH_CONSTRAINT) . As before, both end points (left / right or top / bottom) should be connected to the targets.

layout_constraintWidth_default = spread (default, similar to previous behavior) layout_constraintWidth_default = wrap layout_constraintHeight_default = spread layout_constraintHeight_default = wrap

Wrap provides a significant new widget-resizing behavior, as if wrap_content was used, but limited by related constraints. Thus, the widget will not grow outside of the end points.

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

+10
source share

All Articles