如何在两行以上时纠正ConstraintLayout TextView重叠

时间:2017-11-23 21:58:10

标签: android textview android-constraintlayout

我遇到了约束布局的问题,其中一个文本视图中的文本到达第二行不会按下另一个被限制在其下方的文本视图,直到该行的中间。

我构建了一个包含三个文本视图的简单布局。第一个文本视图位于左侧并具有设置宽度。第二个位于它的右边,在它和它的父母之间。第三个位于第一个位于第二个位置的左下方。

我希望它看起来像: Layout of activity with the three text views as desired

但是,如果我删除了文字" Not Overlapping。"我明白了: Layout of activity with the third text view overlapping the second.

当第一个文本视图不存在时,文本的长度填充两行时,它的变化点(" O" in" Not Overlapping")似乎是: Image showing if the first TextView was not there that the text would wrap perfectly.

那么如何更改此布局,以便即使我在屏幕左侧有文本视图,它也会在文本视图3到达两行时立即向下推?而不是将其推到第二行的一半。

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.testapp.myapplication.MainActivity">

    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="120dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:text="Text View 1"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_view_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#22AA99"
        android:text="Text View 2 Showing problem with overlap. Overlapping. Not O"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintLeft_toRightOf="@id/text_view_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_view_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF00FF"
        android:text="Text View 3"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/text_view_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text_view_2" />

</android.support.constraint.ConstraintLayout>

感谢。

2 个答案:

答案 0 :(得分:21)

我已经找到了

app:layout_constrainedHeight="true"

来自约束布局1.1.0-beta2,它对wrap_content强制执行约束。因此,我觉得这是正确的解决方案,因为这也允许我设置app:layout_constraintBottom_toBottomOf="parent",在视图的底部启用边距。

答案 1 :(得分:3)

删除此行:

app:layout_constraintBottom_toBottomOf="parent"
来自您TextView的身份text_view_3

赞:

<TextView
    android:id="@+id/text_view_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF00FF"
    android:text="Text View 3"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
    app:layout_constraintLeft_toRightOf="@id/text_view_1"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/text_view_2" />