【问题标题】:Make RecycleView show items in the middle of the screen, if the items are few如果项目很少,让 RecyclerView 在屏幕中间显示项目
【发布时间】:2021-02-25 18:16:26
【问题描述】:

我为我的新警报应用程序编写了一个简单的线性 RecycleView。由于我对 Android 编程非常陌生,我遇到了这样一个问题:现在,我拥有的所有项目都是从上到下显示的,即使只有 3 个项目。它看起来不太好,也不方便。如果列表中只有少数项目,有什么方法可以将它们显示在中间,而不是从上到下?

谢谢!!

我目前的主要 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadeScrollbars="true"
        android:scrollbars="vertical"
        android:layout_marginTop="27dp"
        android:layout_marginBottom="36dp"
        android:requiresFadingEdge="vertical|horizontal"
        tools:listitem="@layout/alarms_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-studio android-recyclerview


    【解决方案1】:

    您应该将 recycleView android:layout_height="match_parent" 更改为

     android:layout_height="wrap_content"
    

    然后在您的 RecycleView 中添加这些行。

    app:layout_constraintCircleRadius="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    

    【讨论】:

      【解决方案2】:

      可以将所有recyclerView居中 为此,请更改您的 recyclerView,如下面的 xml 所示

       <androidx.recyclerview.widget.RecyclerView
              android:id="@+id/recycler_view"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              android:fadeScrollbars="true"
              android:scrollbars="vertical"
              android:layout_marginTop="27dp"
              android:layout_marginBottom="36dp"
              android:requiresFadingEdge="vertical|horizontal"
              tools:listitem="@layout/alarms_view" />
      

      当您添加新项目时,您的回收站视图会扩大到屏幕高度

      【讨论】:

      • 这个也可以。但是你能解释一下,宽度“0dp”到底是做什么的? 0dp什么都没有,是不是有什么特殊含义?
      • 官方文档表示不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件。可以通过使用 MATCH_CONSTRAINT 来定义类似的行为,并将相应的左/右或上/下约束设置为“父级”。developer.android.com/reference/androidx/constraintlayout/…
      • 注意 MATCH_CONSTRAINT 是 0dp
      • 根据我的经验,除了以后使用constraintlayout时的性能外,如果你有很多视图,你在使用match_parent时会发现一些布局问题
      【解决方案3】:

      将布局重力添加到父布局并更改recyclerview 高度以包装内容,如下所示:

      <androidx.constraintlayout.widget.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"
          android:layout_gravity="center"
          android:background="@drawable/background"
          tools:context=".MainActivity">
      
      
          <androidx.recyclerview.widget.RecyclerView
              android:id="@+id/recycler_view"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fadeScrollbars="true"
              android:scrollbars="vertical"
              android:layout_marginTop="27dp"
              android:layout_marginBottom="36dp"
              android:requiresFadingEdge="vertical|horizontal"
              tools:listitem="@layout/alarms_view" />
      

      【讨论】:

        猜你喜欢
        • 2017-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多