如何居中于recyclerview的项目? [英] How to center items of a recyclerview?

查看:67
本文介绍了如何居中于recyclerview的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每行中居中放置元素,以便它们类似于本模型中的内容. 我一直在寻找是否有任何布局能以这种方式工作而无需外观. 项目位于其行的中心.

I need to center elements in each row so they will be like in this mockup. I've been searching if there is any layout that works that way without look. items are centered in their rows.

这就是现在在我的代码中的样子.

This is how it looks now in my code.

推荐答案

RecyclerView的项目行布局中放入LinearLayout,然后将android:layout_gravity="center"赋予LinearLayout.

Take LinearLayout in your RecyclerView's item row layout then give android:layout_gravity="center" to LinearLayout.

对于每行图像,您必须采用不同的LinearLayout.

For each row of images you have to take different LinearLayout.

这是示例代码:

 <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image1" />

            <ImageView
                android:id="@+id/image2"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image2" />

           <ImageView
                android:id="@+id/image3"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image3" />

  </LinearLayout>

这篇关于如何居中于recyclerview的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆