android - 具有不断增长的 TextView 的 ConstraintLayout 在需要时声明相等的空间

标签 android android-constraintlayout

是否有可能在 ConstrainLayout 中限制两个 TextView ,以便它们在需要时各自占用 50% 的水平空间,如果不需要,另一个会增长以填充空间。

正确:当文本很短时,textView 不会相互干扰。 equal

B 正确:左侧较短的 textView 为右侧较长的 textView 提供了空间 leftsmall

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

correct

D 不正确:右侧 textView 的增长是以左侧 textView 为代价的。

rightlonger

E 不正确:右边的 textView 垂直增长而不是填充可用的水平空间。

enter image description here

我已经尝试了很多东西。它认为 layout_width="wrap_content"layout_constraintWidth_default="wrap" 是必须的,因为它是 textView 传达它需要多少空间的唯一方式。

实验:

layout_constrainedWidth="true/false"
layout_constraintWidth_default="spread/wrap"
layout_constraintWidth_max="wrap"
layout_constraintHorizontal_weight="1" // with 0dp as width

主要使用水平链中的 textView 进行实验,因为问题中存在对称性,我认为对称解决方案是有意义的。

有人管理过这个还是根本不可能?我不知道为什么它的行为与链中的第二个元素比第一个元素“重”迫使它成为唯一垂直增长的元素。

最佳答案

要解决您的问题,您必须将 TextViews 的宽度设置为 0dp。我添加了一个 Barrier到左边的 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>

cha

关于android - 具有不断增长的 TextView 的 ConstraintLayout 在需要时声明相等的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57676622/

相关文章:

javascript - 在按钮的 OnClick 事件中执行 Javascript 代码

java - 使用约束布局针对不同的屏幕尺寸进行设计

android - 为什么 notifyItemChanged 不起作用

java - ConstraintLayout背景

android - ConstraintLayout中如何用两个view填满屏幕?

java - 以编程方式将 View 添加到约束布局跳转到原点

android - ConstraintLayout - 与自身成比例的宽度/高度?

android - LoopJ AndroidAsyncHttp 和请求 cookie

android - Google map 显示空白 map

android - 获取远程图片导致android中的NetworkOnMainThreadException