首页 > 解决方案 > 防止TextView在ConstraintLayout中与另一个重叠?

问题描述

我遇到了"Den Dolder"TextView 总是与日期 TextView 重叠的问题。我尝试过使用app:layout_constraintRight_toLeftOf="@id/date_txtView",但令人惊讶的是没有效果!

没关系:

没关系

这不行:

那不行

标签: androidandroid-constraintlayout

解决方案


简短的解决方案:

尝试将app:layout_constraintHorizontal_weight="1"和添加android:layout_width="0dp"到您的两个 textViews - 它们在布局中都将采用相同的宽度。

更复杂布局的另一种解决方案:

你需要告诉你的两个文本视图的宽度都是0dp,所以如果文本太长,它们会掉一行。

例如,在您的 2 个 textView 之间设置一个,如下所示:

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    app:layout_constraintEnd_toStartOf="@+id/textView4"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/textView3"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

它看起来像这样(没有 textView 与另一个重叠,它们只是下拉一行):

在此处输入图像描述


推荐阅读