Android 如果需要,使用不断增长的文本视图来约束视图,并要求相等的空间

Android 如果需要,使用不断增长的文本视图来约束视图,并要求相等的空间,android,android-constraintlayout,Android,Android Constraintlayout,在ConstraintLayout中,是否可以对两个文本视图进行约束,以便它们在需要时各自占用50%的水平空间,如果不需要,则另一个文本视图将增长以填充空间 正确:当文本较短时,文本视图不会相互干扰。 B正确:较短的左文本视图为较长的右文本视图提供空间 C正确:平均共享水平空间并垂直增长 D不正确:右文本视图的增长是以左文本视图为代价的 E不正确:右文本视图垂直增长,而不是填充可用的水平空间 我试过很多东西。它认为layout\u width=“wrap\u content”或layo

在ConstraintLayout中,是否可以对两个文本视图进行约束,以便它们在需要时各自占用50%的水平空间,如果不需要,则另一个文本视图将增长以填充空间

正确:当文本较短时,文本视图不会相互干扰。


B正确:较短的左文本视图为较长的右文本视图提供空间

C正确:平均共享水平空间并垂直增长

D不正确:右文本视图的增长是以左文本视图为代价的

E不正确:右文本视图垂直增长,而不是填充可用的水平空间

我试过很多东西。它认为
layout\u width=“wrap\u content”
layout\u constraintWidth\u default=“wrap”
是必须的,因为这是文本视图传达所需空间的唯一方式

试验:

layout_constrainedWidth="true/false"
layout_constraintWidth_default="spread/wrap"
layout_constraintWidth_max="wrap"
layout_constraintHorizontal_weight="1" // with 0dp as width
实验主要是水平链中的文本视图,因为问题中存在对称性,我认为对称解决方案是有意义的


有人做到了吗?还是根本不可能?我不知道为什么它会这样做,因为链中的第二个元素比第一个元素“重”,迫使它成为唯一垂直增长的元素。

您可以使用channing constraint layout属性。我相信这就是你要找的

使用约束布局水平链特性


与链接相结合,您需要的是障碍


要解决问题,必须将
文本视图的宽度设置为0dp。我在左边的
TextView
中添加了一个,这样每当右边的
TextView
需要空间时,它就会占用左边
TextView
的空间。请尝试以下代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/text_barrier"
            app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
            android:id="@+id/text_barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>

你的意思是
布局约束垂直链样式
?,确实有点乱,但是如果你选择了你想要的两个组件并右键单击,你看不出有什么帮助。一些选项会出现。其中一个选项是
。我相信您正在寻找
水平链
。然后,在组件中,只需使用
layout\u width=wrap\u content
至少显示完整的内容。您还可以定义
max_行
。。。任何其他东西。这不起作用。它不符合我在第二张图片中看到的要求。短的左文本视图为长的右文本视图提供了空间->这一个没有填满?是的,我添加了你提供的代码的错误作为一个明确的例子。签出编辑!您只需要在一个链中设置两个文本视图,以便根据内容进行调整(如正确的图像所示)?如果两个文本视图都大于一行,但其中一行长得多,您是否希望:(a)每个文本视图的宽度为50%;或者(b)平衡宽度直到它们达到相同的高度?@AjahnCharles我期待你(a)的选择。这是一个有趣的问题。我认为您最初的示例B&C是两种不同的逻辑情况(取决于总文本长度是否超过1行),这可能会妨碍您找到一个简单的(XML)解决方案。也许有人可以用Java/Kotlin提供解决方案。我个人会寻找一个涵盖所有情况的折衷方案(这就是我选择“平衡”方案的地方;尽管我也没有立即找到解决方案)。也许你可以在两边“保留”一些最小的空间,然后让风景为中间而斗争?!祝你好运你能解释一下你的答案吗?为什么需要
屏障
?似乎
constrainedWidth
很重要!不过,这并不是@Adam想要的答案。我不认为可以使用障碍来实现所需的行为。只要试着在文本视图中插入长文本,你就会发现它不能正确地约束视图。@Adam哦,我忘了。我会努力的,然后告诉你什么时候updated@Adam,我更新了它。这次成功了!祝您有个美好的一天!很抱歉,它不起作用。视图的行为应该是对称的。在视图之间交换文本,视图的宽度也应该交换。在第一个文本视图中输入一个长文本会填满整个屏幕。@Adam,对不起,我不知道如何解决您的问题。我不知道如何使他们的行为对称。我希望这仍然能够帮助您或作为指导方针。如果我能想出一个解决方案,我会告诉你的。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/text_barrier"
            app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
            android:id="@+id/text_barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>