2016-12-18 49 views
1

我有一个简单的布局和回收站。我用适配器喂它,并为回收商设置GridLayoutManager带GridLayoutManager的RecyclerView - 布局项目

<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" /> 

活动

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); 

项目

<LinearLayout 
android:layout_width="300dp" 
android:layout_height="300dp" 
android:background="@drawable/game_border" 
android:gravity="center" 
android:orientation="vertical"> 

我有两个问题:

  1. 有行
  2. 项目不CENTE之间没有空格红色内

我试过Android Studio的所有属性,但它们没有任何效果。我GOOGLE了一下,打开了很多问题,但我仍然没有运气。我应该使用什么魔法财产?

正如您所看到的项目位于屏幕左侧。我想把它移到中心位置。第一排和第二排之间没有空间。

enter image description here enter image description here

+0

任何人有想法,如何居中物品?我会添加单列的另一张照片。 –

+0

也许传递一个自定义布局参数重心=中心?但我没有找到一个二传手。 –

+0

我交换了一个GridView,最后瓷砖居中。 –

回答

0

对于1.有行

之间尝试android:layout_marginBottom="10dp"在item.This的线性布局应该提供每个项目下方的间距没有空间。

对于2.items未行

看起来你给右边距为您recyclerview并没有左缘的中心。

android:layout_marginLeft="20dp" 
android:layout_marginStart="20dp" 

应在两侧给出相等的间距。 或设置

app:layout_gravity="center" 

在项目的线性布局。

+0

广告1)谢谢,这个工程。为什么它不在父容器中工作? –

+0

你是指1或2? –

+0

广告2)这可以使它更好,但我想中心的项目,无论当前的尺寸。 –

2

(1)最简单的解决方案是添加顶部和/或底部边距,就像@suraj说的那样。

关于(2) - 关键问题是您的物品是方形的。为了得到它的工作,设置android:layout_width="match_parent"并改变你的项目的设计,使用两个尺寸的实际较小的尺寸(宽度或高度)(也许你需要做的就是让背景变为正方形)。然后显示项目的LinearLayout中心的内容(您已经这样做了)。你必须这样做,因为GridLayoutManager目前不支持layout_gravity。

+0

谢谢。 GridView现在运行良好。 –

+1

我想通了。但是,既然我遇到了和你一样的问题,并且具有相同的方形视图细节,我决定解决它并帮助世界其他地方。 – Uli

+0

好的,upvoted ... –

相关问题