Ideas:
Set the number of rows in the gridview to one row, and set a horizontalscrollview outside. Set the gridview width in the code.
XML Code
<HorizontalScrollView
android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <GridView android:id="@+id/gv" android:layout_width="wrap_content" android:layout_height="wrap_content" > </GridView> </LinearLayout>
</HorizontalScrollView>
Set in code
// Obtain the pixel density displaymetrics outmetrics = new displaymetrics (); getactivity (). getwindowmanager (). getdefadisplay display (). getmetrics (outmetrics); float density = outmetrics. density; // pixel density viewgroup. layoutparams Params = gridview. getlayoutparams (); int itemwidth = (INT) (100 * density); // The default item width is 100 int spacingwidth = (INT) (5 * density); Params. width = (itemwidth + spacingwidth) * childcount + spacingwidth; gridview. setstretchmode (gridview. no_stretch); // It is set to disable the stretch mode gridview. setnumcolumns (childcount); gridview. sethorizontalspacing (spacingwidth); gridview. setcolumnwidth (itemwidth); gridview. setlayoutparams (Params );