Center children in a gridview

I have 3 columns GridViewand 4 elements.

It so happened that three of them appear in the first line, and the last in the second line, but this is aligned to the left. I just wanted to know if it was possible to center it in GridView. Like a T, I don’t know if I will explain ... xD

Thank: -)

Edit:

This is a layout containing GridView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/section_title_root"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/bg_gd_home_grid">
    <ImageView
    android:src="@drawable/home_grid_txori"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginTop="40dip"
    android:layout_centerHorizontal="true"/>
    <GridView
        android:id="@+id/home_grid"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:numColumns="3"
        android:drawSelectorOnTop="true"
        android:layout_marginBottom="30dip"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="30dip"
        android:padding="5dip"
        android:listSelector="@android:color/transparent"
        android:background="@drawable/bg_home_grid_item"
/>
</RelativeLayout>

By the way, android:layout_centerHorizontal="true"does not work in GridView

Decision:

This is a problem from past years, but I was asked to post a solution, and I had to find my old repo to get it :-)

As I said in the comments, I could not achieve this only in XML format, so I had to trick a fake non-switchable + inaccurate element in third position.

, "ViewHolder".

getView() :

public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            convertView = (RelativeLayout) mInflater.inflate(
                    R.layout.home_grid_item, null);

        }

        /* In order to have 4th item centered in the 2nd row, we create an empty 3rd item */
        if (position == 3) {
            convertView.setFocusable(false);
            convertView.setClickable(false);
        } else {
            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.image);
            imageView.setImageResource(mThumbs[position]);
            TextView textView = (TextView) convertView.findViewById(R.id.title);
            textView.setText(mTitles[position]);

        }

        convertView.setTag(mTabTags[position]);

        return convertView;
    }
+5
2

:

android:gravity="center_horizontal"

xml.

+1

. numcolumn, .

    <GridView
        android:id="@+id/gr_single"
        android:verticalSpacing="@dimen/pay_mode_icon_margin"
        android:horizontalSpacing="@dimen/pay_mode_icon_margin"
        android:columnWidth="@dimen/pay_icon_width"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

java.

mGridView.setNumColumns(mSingleImageViews.size());
mGridView.setAdapter(new ImageAdapter());
0

All Articles