Center elements in GridView

前端 未结 9 1254
挽巷
挽巷 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:27

    Here is my answer:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="100dip"
            android:gravity="center"
            android:numColumns="3"
            android:stretchMode="spacingWidthUniform"
            android:verticalSpacing="10dip" />
    </LinearLayout>
    

    Don't use padding or horizontalSpacing if you don't need it as it may cause problems with other views.

    Note also that fill_parent is already deprecated.

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

    I hope this will solve your problem:

      <LinearLayout
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        <GridLayout
            android:id="@+id/gridlayou4x3"
            android:rowCount="4"
            android:columnCount="3"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="80"
            android:minWidth="25px"
            android:minHeight="25px" />
      </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 20:31

    Issue occurred when I updated the gridview adapter from:

            gridView = inflater.inflate(R.layout.mobile, null);
    

    to

            gridView = inflater.inflate(R.layout.mobile, parent, false);
    

    so I just reversed back using a try catch if the old (null) version failed.

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

    well, in case you are using a custom adapter thing to inflate single rowview inside your gridview then try to add android:gravity="center" to the layout of your single_row.. this will do the trick hopefully :) i.e,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/ivClrBlock"
            android:layout_width="88dp"
            android:layout_height="88dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            />
    
        <TextView
            android:id="@+id/tvClrName"
            android:layout_width="88dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="@string/color_name"
            android:textSize="15sp" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 20:40

    Here is what I did to get the items in the gridview to center (notice the stretchmode, columnwidth, gravity and horizontal and vertical spacing):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5dp" >
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="100dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="3"
            android:stretchMode="spacingWidthUniform"
            android:verticalSpacing="10dp" />
    </LinearLayout>
    

    It took me a while to figure it out, but messing with those different values I was able to get them to center.

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

    Need to set the following attributes:

    android:stretchMode="columnWidth"
    

    ...and...

    android:gravity="center"
    
    0 讨论(0)
提交回复
热议问题