How to cenralize last uneven row in GridView in android?

…衆ロ難τιáo~ 提交于 2019-11-28 12:07:56

I have the same problem, but I managed this using two gridviews, with first gridview displaying the rows except the last row, with the second gridview displaying the last row. This is the simplest way to achieve what I need. I have not find a better solution. Looking forward to see a nice and simple way also.

you could try using invisible elements, it not the most efficient solution but is simple, you can put two elements, one at the start, one at the end of the row and make them invisible

For those who are still looking for a solution for this here's my take for this, create a TableLayout and, and for each TableRow set its weigth to android:weightSum="4" and dont forget to add android:gravity="center"

For complete code check this gist for reference.

You could center the grid row inside of a relative layout and then add android:layout_centerHorizontal="true" to the alphabet ImageView's. Assuming that they're ImageView's.

So your GridView would be wrapped in something like this:

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_cotent"
    android:orientation="horizontal" >

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="70dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="20dp"
        android:gravity="center"
        android:stretchMode="spacingWidthUniform" >  

    </GridView>
</RelativeLayout>

And the individual alphabet images would need android:layout_centerHorizontal="true" like this:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
</ImageView>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!