Center elements in GridView

前端 未结 9 1260
挽巷
挽巷 2020-12-29 19:44

I have GridView with 6 images in 2 columns. I want to display this 6 images in the middle of the screen. I set gravity properties to center but this center elements only hor

相关标签:
9条回答
  • 2020-12-29 20:40

    I found that you just have the gridlayout wrap content on height and width.

    android:layout_centerHorizontal="true"
    

    It will center itself for the size it needs but if you have it matching the parent. You are technically centering it based with a lot of parent empty space offsetting your children.

    0 讨论(0)
  • 2020-12-29 20:43

    Try to change this:
    android:layout_height="fill_parent"
    into
    android:layout_height="wrap_content"

    You are now telling your grid view to fill it's parent height and width fully.

    0 讨论(0)
  • 2020-12-29 20:48

    Try wrapping your GridView in a LinearLayout that has it's child elements centered and change your gridview's layout_height to "wrap_content". ex:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:gravity="center"
      android:layout_gravity="center">
        <GridView 
          android:id="@+id/homeGridView"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:numColumns="3"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:stretchMode="columnWidth"
          android:gravity="center" />
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题