Android 最后一项居中的GridView

Android 最后一项居中的GridView,android,gridview,Android,Gridview,我正在做一个GridView(非动态),它包含13个值,我想把它放在两列上,它看起来像: 这是我的代码: <?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-a

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

这是我的代码:

<?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>
这是一个完全实现您的场景的示例项目

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


在两列网格中,宽度将是父网格的一半。

我面临同样的问题:我有一个两列的网格布局和奇数个元素。最后一个元素必须居中

我使用自定义的
SpanSizeLookup
解决了这个问题,并在最后一个元素上添加了一些边距:

在RecyclerView/活动中:

val layoutManager = GridLayoutManager(context, 2)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup()
{
    override fun getSpanSize(position: Int): Int
    {
        return if (position == 0 || isLastRowAndShouldBeCentered(position)) 2 else 1
    }
}

myRecyclerView.layoutManager = layoutManager
在适配器中:

override fun onCreateViewHolder(parent: ViewGroup): ViewHolder
{
    // Save the width of the container
    this.containerWidth = parent.measuredWidth

    // Create your ViewHolder here
}

override fun onBindViewHolder(holder: ViewHolder, position: Int)
{
    ...

    /**
     * This margin will be exactly 1/4 of the container width.
     * This way, the element will be displayed as follows :
     * [ BLANK 1/4 SPACE ] [ ELEMENT 1/2 WIDTH ] [ BLANK 1/4 SPACE ]
     */
    if (isLastRowAndShouldBeCentered(position))
    {
        val params = viewHolder.view.layoutParams as GridLayoutManager.LayoutParams
        val marginSize = this.containerWidth / 4 // Re-use the container width here
        params.leftMargin = marginSize
        params.rightMargin = marginSize
        viewHolder.view.layoutParams = params
    }
}
显然,
islastrowandshouldbecented
由您来实现(返回一个布尔值,指示我们是否在最后一个元素上,并且元素的数量是否为奇数)


希望它能帮助别人

我最近实现了类似的功能。我对代码做了一些修改,并与大家分享。谢谢。。。。我很高兴能帮上忙:)@步枪队你能检查一下吗?please@AndroidWannabe好的,我查一下asap@thetonrifles谢谢你,伙计。。。我认为它可以做一些像全球或全球类连接到蓝牙或其他东西。。。但问题是当活动进入暂停状态时:(。。。
override fun onCreateViewHolder(parent: ViewGroup): ViewHolder
{
    // Save the width of the container
    this.containerWidth = parent.measuredWidth

    // Create your ViewHolder here
}

override fun onBindViewHolder(holder: ViewHolder, position: Int)
{
    ...

    /**
     * This margin will be exactly 1/4 of the container width.
     * This way, the element will be displayed as follows :
     * [ BLANK 1/4 SPACE ] [ ELEMENT 1/2 WIDTH ] [ BLANK 1/4 SPACE ]
     */
    if (isLastRowAndShouldBeCentered(position))
    {
        val params = viewHolder.view.layoutParams as GridLayoutManager.LayoutParams
        val marginSize = this.containerWidth / 4 // Re-use the container width here
        params.leftMargin = marginSize
        params.rightMargin = marginSize
        viewHolder.view.layoutParams = params
    }
}