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

Mun*_*Rae 10 android textview android-constraintlayout

我有约束布局的问题,即文本在一个文本视图到达第二线不会下推被限制在它下面的另一个文本视图,直到该行的中间.

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

我希望它看起来像: 根据需要使用三个文本视图布局活动

但是,如果我删除文本"不重叠".我明白了: 第三个文本视图与第二个文本视图重叠的活动布局.

在它(在"不重叠"的"O")的变化点似乎是当文本的长度填充在第一文本查看是不是有两条线: 图像显示第一个TextView是否不存在文本将完美包装.

那么如何更改此布局,以便即使我在屏幕左侧有文本视图,它也会在文本视图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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Mun*_*Rae 30

我发现了

app:layout_constrainedHeight="true"

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

  • 对于horizental重叠,我们使用这个'app:layout_constrainedWidth ="true"` (4认同)
  • MungoRae 和@tamtom 你的回答对我很有帮助。多谢。 (2认同)