views:

92

answers:

2

What I want is a central column using GridView, but everything I try does not work. It always aligns to the left. I have tried messing around with layout_gravity and gravity to no avail.

I have a GridView in a LinearLayout:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
    <GridView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:numColumns="1"
        android:columnWidth="100dp"
        android:stretchMode="spacingWidth"
    />
</LinearLayout>
A: 
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
android:gravity="center_vertical">
    <GridView
        android:layout_width="5px"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:numColumns="1"
        android:columnWidth="100dp"
        android:stretchMode="spacingWidth"
    android:layout_gravity="1"/>
</LinearLayout>

This works fine for me. I changed the gravity to center_vertical and set the gravity of the gridview to 1. I also changed the gridview width just to see if it worked or not. With fill_parent it was hard to see if it was centered or not.

Pandoro
Android Plugin for Eclipse has a fit when I try and layout_gravity="1", so I cannot really compile your example.
kuroutadori
Weird I copied that straight from my eclipse. Worked just fine for me.
Pandoro
Yes very weird.
kuroutadori
A: 

I ended up doing this instead:

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
    <GridView
      android:id="@+id/grid_accounts"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:verticalSpacing="25dp"
      android:listSelector="@color/black"
    />
</FrameLayout>

And made time set the gravity of the images to center

kuroutadori