【问题标题】:How do i remove the spaces between RecycleView items?如何删除 RecyclerView 项目之间的空格?
【发布时间】:2020-07-07 13:00:58
【问题描述】:

我有文本的回收视图,但无法摆脱它们之间的空格。我尝试将填充归零但没有。这是回收站 xml:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

它的约束布局的子级

这是列表项的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot

【问题讨论】:

  • 能把你的子item布局的全部代码展示出来吗,如果能展示截图就好了
  • 添加了代码和屏幕截图。您可以在列表中看到两个文本,在屏幕的中心以及它们的距离
  • 在您的列表项 xml 中,您应该将 ConstraintLayout layout_height 设置为“wrap_content”而不是“match_parent”。
  • 就是这样,谢谢大家!
  • @Userthatisnotauser 我看到类似的答案已经被接受,所以不需要。

标签: android android-recyclerview space


【解决方案1】:

将XML中ConstraintLayout的高度改为wrap_content

改变高度

android:layout_height="match_parent"

android:layout_height="wrap_content"

【讨论】:

    【解决方案2】:

    在约束布局中使matchparent 换行

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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="wrap_content">
    
    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】: