避免在ConstraintLayout中重叠

时间:2018-11-22 00:51:35

标签: 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>

这是它的显示方式:

Actual

它应该是:

Ideal

我需要解决什么问题?我已经尝试过使用准则和水平偏差,还尝试使用RelativeLayout而不是ConstraintLayout对其进行约束,但到目前为止没有任何帮助。

3 个答案:

答案 0 :(得分:1)

您可以尝试将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 :(得分: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>

答案 2 :(得分:1)

如果您不关心第二个视图的宽度为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"

enter image description here

enter image description here

这是全部:

<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" />
相关问题