多于两行时如何纠正ConstraintLayout TextView重叠 [英] How to correct ConstraintLayout TextView overlapping when more than two lines

查看:208
本文介绍了多于两行时如何纠正ConstraintLayout TextView重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在约束布局方面遇到问题,其中一个文本视图中的文本到达第二行将不会向下推另一个文本视图,该文本视图被限制在其下方,直到该行的中间为止.

I am having a problem with constraint layout where the text in one text view reaching the second line will not push down another text view that is constrained to be below it, until the middle of the line.

我用三个文本视图构建了一个简单的布局.第一个文本视图位于左侧并具有设置的宽度.第二个位于它与它的父对象之间的右边.第三个位于第二个下方,位于第一个的左侧.

I have built a simple layout with three text views. The first text view sits on the left and has a set width. The second one sits to the right of this, between it and its parent. The third sits below the second and to the left of the first.

我希望它看起来像:

I want it to look like:

但是,如果我删除文本"Not Overlapping".我得到:

However if I remove the text "Not Overlapping." I get:

它的改变点(不重叠"中的"O")似乎是当第一个文本视图"不存在时文本的长度填充两行时:

The point at which it changes (The "O" in "Not Overlapping")appears to be when the length of the text fills two lines when the first Text View is not there:

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

So how do I change this layout so that even when I have a text view on the left side of the screen it will push Text View 3 down as soon as it reaches two lines? As opposed to pushing it down half way through the second line.

我的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>

谢谢.

推荐答案

从那时起我就找到了

app:layout_constrainedHeight="true"

来自约束布局 1.1.0-beta2 这会对wrap_content施加约束.因此,我认为这是正确的解决方案,因为这还允许我设置app:layout_constraintBottom_toBottomOf="parent",从而在视图底部启用边距.

from constraint layout 1.1.0-beta2 which enforce constraints on wrap_content. I therefore feel this is the correct solution as this also allows me to set the app:layout_constraintBottom_toBottomOf="parent" which enables a margin at the bottom of the view.

这篇关于多于两行时如何纠正ConstraintLayout TextView重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆