避免 ConstraintLayout 中的重叠

raj*_*ath 3 android android-layout android-constraintlayout

我有以下布局:

<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>
Run Code Online (Sandbox Code Playgroud)

这就是它的显示方式:

实际的

真的应该是:

理想的

我需要什么来解决这个问题?我试过使用指南和水平偏差,也试过用 RelativeLayout 而不是 ConstraintLayout 来约束它,但到目前为止没有任何帮助。

Aar*_*ron 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"/>
Run Code Online (Sandbox Code Playgroud)

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


Ben*_* P. 5

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

在此输入图像描述

如果您需要wrap_content行为,那么您可以执行与上面相同的操作,但另外添加这两个属性:

app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

一切都在这里:

<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" />
Run Code Online (Sandbox Code Playgroud)