android - ConstraintLayout 中元素的叠加

标签 android android-layout android-constraintlayout

我有以下布局:

<?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"
    android:background="#EEEEEE"
    >

    <ImageView
        android:layout_width="4dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        />

    <TextView
        android:text="12345678911131517192123252729313335373941434547495153555759616365676971737577798183858789"
        android:id="@+id/title"
        android:textSize="15sp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="16dp"
        />

    <TextView
        android:text="  text text"
        android:id="@+id/prev"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="WHY WHY WHY"
        app:layout_constraintTop_toBottomOf="@+id/prev"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        />


</android.support.constraint.ConstraintLayout>

我们有以下结果: image with this issue

另外,如果我们把第一个TextView的文本改成:“1234567891113151719212325272931333537394143454749515355575961636567697173757779818385878991”(也就是多加2个数字),最后一个 TextView 停止关闭前一个。

此外,如果我们在最后一个 TextView 中将 layout_marginBottom 属性更改为 0dp(文本将像示例中那样保留),问题也会消失。这个问题的原因是什么?如何解决?

更新:

在左侧添加了一个具有 match_parent 高度的路径。因此不能在 ConstraintLayout 中使用 paddingBottom。在 RecyclerView 中使用布局,这就是底部元素需要 layout_marginBottom 的原因。

最佳答案

要么从 textview3 中删除 app:layout_constraintBottom_toBottomOf="parent",要么将布局的高度更改为 match_parent

希望对您有所帮助! :)

关于android - ConstraintLayout 中元素的叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45904442/

相关文章:

java - 如何在textview android上禁用仅剪切选项

java - Android - Java 堆栈与 native 堆栈

android - 如何将约束布局包含到另一个约束布局并在每个约束布局之间设置约束

android - 折叠工具栏布局和约束布局

android - 你能动态创建一个 ConstraintLayout 链吗?

java - Android给ImageView添加边框图片

android - 从 Android 中的图像创建视频文件

Android:以编程方式使用 fragment

android - 减小 ImageSpan 的高度和宽度

java - 在 Fragments Android 中使用 ConstraintLayout 时的空 UI