避免 ConstraintLayout 中的重叠问题

3
我有以下布局:
<android.support.constraint.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="wrap_content"
android:padding="10dp">

<TextView
    android:id="@+id/itemKey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    app:layout_constraintStart_toStartOf="parent"
    tools:text="Recipient:"/>

<TextView
    android:id="@+id/itemValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:textColor="@color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toRightOf="@id/itemKey"
    tools:text="loooooooooonnnnnnngemmmmmmmmaaaaaaaiiilll@gmail.com"/>

</android.support.constraint.ConstraintLayout>

这是英文原文,意思是“这就是它的显示效果:”,其中保留了HTML标签。

Actual

它真正应该是:

Ideal

我需要怎样才能解决这个问题?我已经尝试使用指南线和水平偏差,也尝试使用RelativeLayout来约束它,但是到目前为止都没有帮助。

1
你可以将 TextView 的宽度设置为 0dp,或使用 app:layout_constrainedWidth="true" 来强制执行约束。 - Sadegh
3个回答

6
您可以尝试将itemValue的宽度设置为0,并使用layout_constraintStart_toEndOf代替:
<TextView
        android:id="@+id/itemValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/itemKey"
        tools:text="loooooooooonnnnnnngemmmmmmmmaaaaaaaiiilll@gmail.com"/>

如果您想将文本对齐到末尾,您可能需要使用android:gravity="right|end"


1
谢谢你,Aaron。虽然还有其他的答案也能起作用,但是你建议使用gravity标志让我最终解决了问题。 - Rajath

5
如果您不介意第二个视图的宽度为wrap_content,您可以进行一些小的更改。将宽度更改为0dp而不是wrap_content,并将app:layout_constraintLeft_toRightOf更改为app:layout_constraintStart_toEndOf(具有相同的值)。这将使该视图始终与剩余的水平空间完全一样大:

enter image description here

如果您需要wrap_content行为,则可以执行与上述相同的操作,但还要添加这两个属性:
app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap"

这里输入图片描述

这里输入图片描述

全文如下:

<TextView
    android:id="@+id/itemValue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:textColor="@color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toEndOf="@id/itemKey"
    app:layout_constraintWidth_default="wrap"
    tools:text="loooooooooonnnnnnngemmmmmmmmaaaaaaaiiilll@gmail.com" />

1
这应该可以解决您的问题。如果您希望文本换行,可以删除maxLines=1条件。
 <android.support.constraint.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="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/itemKey"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="Recipient:"/>

    <TextView
        android:id="@+id/itemValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/itemKey"
        tools:text="loooooooooonnnnnnngemmmmmmmmaaaaaaaiiilll@gmail.com"/>

</android.support.constraint.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接