How to cenralize last uneven row in GridView in android?

后端 未结 4 1932
北海茫月
北海茫月 2020-12-11 03:03

I have a GridView , which displays pictures of alphabets what happens is , every time in the last row there are less no of alphabets. and the last row is left a

相关标签:
4条回答
  • 2020-12-11 03:31

    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.

    0 讨论(0)
  • 2020-12-11 03:47

    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

    0 讨论(0)
  • 2020-12-11 03:51

    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>
    
    0 讨论(0)
  • 2020-12-11 03:57

    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.

    0 讨论(0)
提交回复
热议问题