Android: how to force gridview to wrap_content width?

My GridView contains fixed-width columns with a fixed horizontal spacing. If the columns are not enough to fill the screen horziontally, I would like my width of the GridView to turn to its contents and center the vertical on the screen.

However, regardless of the number of columns that I use, the width of the GridView increases to fill the screen. The attached image shows this, where the green GridView fills the screen horizontally, despite having only 3 columns, and its width is set to "wrap_content".

public class Temp extends Activity
{
    private GridView grid;
    private int columnWidth = 80;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        View view = getLayoutInflater().inflate(R.layout.gridview, null);
        grid = (GridView) view.findViewById(R.id.grid);
        grid.setColumnWidth(columnWidth);
        grid.setAdapter(new GridAdapter());

        setContentView(view);
    }

    class GridAdapter extends BaseAdapter 
    {
        public GridAdapter() 
        {
        }

        public int getCount() 
        {
            return 3;
        }

        public Object getItem(int position) 
        {
            return null;
        }

        public long getItemId(int position) 
        {
            return position;
        }

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

            if (convertView == null) 
            {
                ret = new ImageView(Temp.this);
                ret.setLayoutParams(new GridView.LayoutParams(columnWidth, 100));
                ret.setBackgroundColor(Color.WHITE);
            }
            else
            {   
                ret= convertView;
            }

            return ret;
        }
    }
}




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FF0000"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView 
        android:id="@+id/grid"
        android:background="#00FF00"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:verticalSpacing="2dip"
        android:horizontalSpacing="2dip"
        android:stretchMode="columnWidth"
        android:gravity="center">
    </GridView>
</RelativeLayout>

enter image description here

+5
source share
3 answers

GridView , . , auto_fit, , GridView, , . , , - ImageView . , . , , 1 , LinearLayout, . Linear Layout ImageViews . , .

+1

GridView = 1 0,5 GridView. .

<LinearLayout 
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <GridView
        android:id="@+id/myGridView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:scrollbars="none"
        android:verticalSpacing="10dp" >
    </GridView>

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
+1

.

In the end, it was easier for me to achieve the desired results by setting the width and location of the X grid programmatically - using the setX (float) method to center the grid.

0
source

All Articles