[英]RecyclerView with GridLayoutManager - layouting items
I have a simple layout with a recycler. 我的回收站布局很简单。 I feed it with Adapter and set GridLayoutManager
for the recycler. 我用Adapter喂它,并为回收站设置GridLayoutManager
。
<android.support.v7.widget.RecyclerView
android:id="@+id/scripts_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:verticalSpacing="20dp"
android:clipToPadding="false"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
tools:listitem="@layout/tmpl_script"
android:paddingBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingTop="30dp"
app:layout_gravity="center" />
Activity 活动
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.scripts_recycler_view);
recyclerView.setAdapter(adapter);
int columns = getResources().getInteger(R.integer.scripts_columns);
recyclerView.setLayoutManager(new GridLayoutManager(this, columns));
recyclerView.setHasFixedSize(true);
Item 项目
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/game_border"
android:gravity="center"
android:orientation="vertical">
I have two problems: 我有两个问题:
I tried all properties in Android Studio but they do not have any effect. 我尝试了Android Studio中的所有属性,但是它们没有任何作用。 I googled, opened many questions but I still have no luck. 我用谷歌搜索,打开了许多问题,但我仍然没有运气。 What magic property shall I use? 我应该使用什么魔法财产?
As you can see items are positioned on left side of the screen. 如您所见,项目位于屏幕左侧。 I want to move it to center. 我想将其移到中心。 And there is no space between first and second row. 第一和第二行之间没有空间。
The easiest solution for (1) is to add top and/or bottom margin, like @suraj said. (1)最简单的解决方案是增加顶部和/或底部边距,如@suraj所说。
Regarding (2) - the critical issue is that your items are square. 关于(2)-关键问题是您的物品是正方形的。 To get it working, set android:layout_width="match_parent"
and change the design of your items to use the actual smaller dimension (width or height) for both dimensions (maybe all you need to do in your case is to make the background square). 要使其正常工作,请设置android:layout_width="match_parent"
并更改商品的设计,以将实际较小的尺寸(宽度或高度)用于这两个尺寸(也许您需要做的就是使背景变为正方形) )。 Then display the content centered within the item's LinearLayout (you are already doing this). 然后显示位于项目的LinearLayout中心的内容(您已经在执行此操作)。 You have to do it this way because GridLayoutManager does not support layout_gravity at this time. 您必须这样做,因为GridLayoutManager目前不支持layout_gravity。
For 1.there is no space between rows 对于1.行之间没有空格
Try android:layout_marginBottom="10dp"
in the linear layout of item.This should provide the spacing below each item. 在项目的线性布局中尝试android:layout_marginBottom="10dp"
。这应在每个项目下方提供间距。
For 2.items are not centered within the row 对于2.项目不在行内居中
Looks like you have given right margin to your recyclerview and no left margin. 看来您已为自己的recyclerview设置了适当的边距,而没有剩余的边距。
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
should give equal spacing on both sides. 两侧应等距。 or set 或设置
app:layout_gravity="center"
in linear layout of the item. 在项目的线性布局中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.