gpt4 book ai didi

android - 通过另一个 View 限制 ConstraintLayout 中的宽度

转载 作者:IT老高 更新时间:2023-10-28 22:21:09 24 4
gpt4 key购买 nike

是否有可能(在 ConstraintLayout 中)让 View 仅在其右侧有空间容纳另一个 View 时才增长?

用例是有一个 valueunit TextViews 并排放置。只要有 unit 的空间,value TextView 应该能够增长。如果没有足够的空间,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" />

最佳答案

是的,您可以使用 ma​​tch_constraint (0dp) 等于其他布局的 match_parent,因此通过使用 ma​​tch_constraint 我们为第一个 View 设置权​​重,它将占据所有可用空间还要加

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>

从网站上得到了一些解释

更好的 View 尺寸控制

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

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

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

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

关于android - 通过另一个 View 限制 ConstraintLayout 中的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44409421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com