通过另一个视图限制ConstraintLayout中的宽度 [英] Restrict width in ConstraintLayout by another view
问题描述
只要在右边有另一个视图的空间,是否有可能(在ConstraintLayout
中)让视图增长?
Is there a possibility (in ConstraintLayout
) to let a view grow only as long as there is space for another view at his right?
用例是彼此之间分别具有value
和unit
TextView.只要unit
有空间,value
TextView应该应该能够增长.如果没有足够的空间,则应剪切value
.
The use case is to have a value
and unit
TextViews besides each other. The value
TextView should be able to grow as long as there is space for the unit
. If there is not enough space, the value
should be cut.
我已经用链条和其他方法尝试过,但是无法完成. value
不会停止增长,因此unit
不再可见.这是当前代码:
I've tried it with chains and some other things but can't get it done. The value
doesn't stop growing and then the unit
is not visible anymore. 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" />
推荐答案
是的,您可以使用 match_constraint (0dp)(对于其他布局,该值等于match_parent),因此可以使用 match_constraint 我们为第一个视图设置权重,该视图将占用所有可用空间,并添加
yes you can by using match_constraint (0dp) which equal to match_parent for other layout, so by using match_constraint we set weight for first view which will occupies all available space also add
app:layout_constraintWidth_default="wrap"
将默认宽度行为应用为wrap_content
to apply default width behavior as 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>
从网站上得到了一些解释
got some explanation from site
更好的视图尺寸控件
将维度设置为0dp (MATCH_CONSTRAINT)时的新可用行为.和以前一样,两个端点(左/右或上/下)都需要连接到目标.
The new available behaviors when a dimension is set to 0dp (MATCH_CONSTRAINT). As before, both endpoints (left/right or top/bottom) need to be connected to targets.
layout_constraintWidth_default =传播(默认,类似于之前的行为) layout_constraintWidth_default =包装 layout_constraintHeight_default =传播 layout_constraintHeight_default =包装
layout_constraintWidth_default = spread (default, similar to the previous behavior) layout_constraintWidth_default = wrap layout_constraintHeight_default = spread layout_constraintHeight_default = wrap
Wrap 提供了重要的新行为,窗口小部件的大小如同使用wrap_content一样进行了调整,但受到连接的约束的限制.因此,小部件将不会超出端点.
Wrap provides a significant new behaviour, with the widget resizing as if wrap_content was used, but limited by the connected constraints. A widget will thus not grow beyond the endpoints.
http://tools.android.com/recent/constraintlayoutbeta5isnowavailable
这篇关于通过另一个视图限制ConstraintLayout中的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!