ConstraintLayout中的RecyclerView与其他元素重叠

时间:2017-11-23 12:06:08

标签: android android-layout android-recyclerview android-constraintlayout

我正在使用以下组件进行简单的列表活动:EditText,RecyclerView,ProgressBar和Textview。一切都运行得很好,除了与EditText重叠并且“超出”屏幕底部的RecyclerView,所以最后一项被部分剪掉(参见截图)。 我做错了什么?

MainActivity:

<?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="match_parent"
    tools:context="jacopo.com.flickrgallery.GalleryActivity">

    <EditText
        android:id="@+id/search_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:hint="search by tag..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/gallery_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gallery_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text" />

    <TextView
        android:id="@+id/gallery_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        tools:text="error" />

</android.support.constraint.ConstraintLayout>

recyclerview overlapping editext recyclerView going outside the screen

9 个答案:

答案 0 :(得分:28)

您正在android:layout_height="wrap_content"使用RecyclerView 但看起来你需要限制身高。试试android:layout_height="0dp"。在这种情况下,它等于app:layout_constraintTop_toBottomOf="@+id/search_text"app:layout_constraintBottom_toBottomOf="parent"

答案 1 :(得分:5)

尝试关注,您必须将topConstraint的{​​{1}}设置为recyclerview

edittext

输出

See

答案 2 :(得分:1)

我遇到了完全相同的问题,然后添加

app:layout_constrainedHeight="true"

到RecyclerView解决了。

答案 3 :(得分:0)

在RecyclerView中添加:

app:layout_constraintStart_toEndOf="@+id/search_text"

答案 4 :(得分:0)

将您的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="match_parent">

    <EditText
        android:id="@+id/search_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:hint="search by tag..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/gallery_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gallery_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toEndOf="@+id/search_text" />

    <TextView
        android:id="@+id/gallery_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        tools:text="error" />

</android.support.constraint.ConstraintLayout>

试试这个并告诉我这是否适合您。

答案 5 :(得分:0)

您将相同的ID传递给每个视图,然后使用此修改代码。

 <?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="match_parent">

    <EditText
        android:id="@+id/search_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:hint="search by tag..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ProgressBar
        android:id="@+id/gallery_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        tools:layout_editor_absoluteX="376dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gallery_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/gallery_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        tools:text="error"
        tools:layout_editor_absoluteX="385dp" />

</android.support.constraint.ConstraintLayout>

快乐的编码!!

答案 6 :(得分:0)

只需使用

app:layout_constraintVertical_bias="0.0"

在recyclerView标签中。

希望这会有所帮助。

答案 7 :(得分:0)

由于某种原因,添加适当的约束并将高度设置为0dp对我不起作用(即使在大多数情况下也应如此)

因此,我将框架布局添加为回收站视图的父级,并将回收站视图的高度和宽度设置为match_parent,将约束添加到框架布局。

这停止了重叠的问题

答案 8 :(得分:0)

我在构建音乐播放器应用程序时遇到了这个问题我解决了我的问题以提供 recyclerview 0dp IDK 的高度它是如何工作的,但我的问题已解决