最后一项居中的GridView [英] GridView with last item centered
本文介绍了最后一项居中的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个GridView(不是动态的),它包含13个值,我想把它放在两列,它看起来像:
I'm doing a GridView (not dynamic), that contains 13 values and I want to put it on two columns and it looks like :
这是我的代码:
And this is my code :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvCard"
android:text="A"
android:layout_marginRight="@dimen/_7sdp"
android:textSize="@dimen/_27sdp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ivCard"
android:layout_toStartOf="@+id/ivCard" />
<ImageView
android:id="@+id/ivCard"
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_28sdp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
我用 Span
为什么不把它放在中间?
And I used Span
why isn't putting it on the middle?
GridLayoutManager recycleLayoutManager = new GridLayoutManager(mContext, 2,GridLayoutManager.VERTICAL, false);
recycleLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// grid items to take 1 column
if (mList.size() % 2 == 0) {
return 1;
} else {
return (position == mList.size()-1) ? 2 : 1;
}
}
});
推荐答案
Here it is a sample project implementing exactly your scenario.
基本上你必须用宽度可以在
Basically you have to wrap your cards with a layout having width to be defined programmatically in onCreateViewHolder
method of the adapter.
在2列网格中,宽度将是父级的一半。
In a 2 columns grid, width will be half of parent.
这篇关于最后一项居中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文