在使用layout_constrainedWidth时,如何防止ConstraintLayout链中的TextView将其他TextView超出其约束?

use*_*456 11 android android-layout android-support-library android-constraintlayout

我的最终目标是在左对齐的水平链中放置两个单行TextView,允许它们两个生长以填充剩余空间,必要时将其均匀分割,在没有空间时进行椭圆化处理.

视觉辅助: 在此输入图像描述

这是我试图完成的布局代码:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text="@tools:sample/lorem"
        app:layout_constrainedWidth="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/textView2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text="@tools:sample/lorem"
        app:layout_constrainedWidth="true"
        app:layout_constraintStart_toEndOf="@id/textView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,我在水平链中布置了两个textview.我已经将链式样式设置为打包,以便它们保持在一起.我将水平偏置设置为0,以便链条保持对齐.我已经将宽度设置为wrap_content,以便它们在文本很短时不会拉伸,并且我还设置为app:layout_constrainedWidth="true"在文本很长时它们不会越过边界.这几乎完全符合我的要求,除非 textView2中的文本增长.随着textView1的增长,它将textView2向右推,直到它达到其约束,此时它会进行椭圆化(如预期/所需),但textview2的情况也是如此.随着textView2的增长,它会延伸到右边的房间,但是一旦它达到它的约束,而不是椭圆化,它会保持拉伸并开始将textView1推向左边,直到它完全不再可见.

视觉辅助(实际行为):

在此输入图像描述

我试图layout_constraintHorizontal_weight在每个视图上使用设置为.5之类的东西,但除非我将两个视图宽度都更改为0dp(match_constraints),这会破坏两个视图都有短文本的情况(它会在两个文本之间添加额外的空间)视图).

感觉就是当你结合width=wrap_content使用时layout_constrainedWidth=true,重量值会被忽略.这仅仅是ConstraintLayout的限制吗?我花了很多时间试图找到一种方法来实现这项工作,而现在它看起来似乎不太可能.我已经退回到使用LinearLayout并做出一些设计妥协,但如果有人有任何想法,我真的很想让它工作.谢谢!

Ore*_*kay 5

如果有人仍在寻找答案,我认为以下代码会有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintWidth_max="wrap"
        app:layout_constraintWidth_percent="0.5"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text2"
        app:layout_constraintHorizontal_chainStyle="packed"
        android:background="#ff0000"
        android:text="This is what you are looking for ?"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintWidth_max="wrap"
        app:layout_constraintWidth_percent="1"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="#ee0"
        android:text="This is a Long Text TextView2 And not something else"
        />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)