【问题标题】:Center alignment of RecyclerView in androidandroid中RecyclerView的居中对齐
【发布时间】:2015-11-10 19:56:30
【问题描述】:

我正在关注本教程 http://www.exoguru.com/android/ui/recyclerview/custom-android-grids-using-recyclerview.html 并且我已将元素的总数最小化为 4。 我想在屏幕中间查看recyclerView。但是当我使用“wrap_content”作为它的高度时,recyclerView 显示在屏幕顶部。但是如果我指定它的高度(例如:“350dp”)它显示在我想要的屏幕中心。使用“wrap_content”时出了什么问题

【问题讨论】:

  • 如果您一次只显示 4 个项目,为什么要使用 RecyclerView?这些物品被回收的机会接近 0。

标签: android xml android-recyclerview


【解决方案1】:

RecyclerView 的概念应该是显示很多项目,这些项目溢出屏幕。并回收(重用)一组最小的视图来显示一堆项目。

关于您的案例,项目太少而无法填满屏幕。所以这并不奇怪。

【讨论】:

    【解决方案2】:

    “Wrap_content”不适用于 RecyclerViews(如 here 所述),但在 this site 上有一些解决此问题的方法。

    如果您只想将 RecyclerView 垂直居中,您可以执行以下操作:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <Space
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
      <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"/>
    
      <Space
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2021-09-26
      • 2015-07-25
      • 2014-09-01
      • 2011-12-02
      • 1970-01-01
      • 2021-06-01
      相关资源
      最近更新 更多