最后一项居中的 GridView [英] GridView with last item centered

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

问题描述

我正在做一个包含 13 个值的 GridView(非动态),我想把它放在两列上,它看起来像:

I'm doing a GridView (not dynamic), that contains 13 values and I want to put it on two columns and it looks like :

这是我的代码:

<?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.

基本上你必须用一个布局来包裹你的卡片,布局的宽度可以在适配器的 onCreateViewHolder 方法中以编程方式定义.

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屋!

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