The Main.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <gridview android:id= "@+id/mygridview" android:layout_width= "Fill_ Parent " android:layout_height=" wrap_content " android:numcolumns=" 3 " android:stretchmode=" ColumnWidth "> </GridView></LinearLayout>
The Grid_layout.xml code is as follows:
<?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:o rientation= "vertical" > <imageview android:id= "@+id/img" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:scaletype= "center" android:padding= "3px"/></ Linearlayout>
. Java code is as follows:
Package Org.lxh.demo;import Java.lang.reflect.field;import Java.util.arraylist;import java.util.HashMap;import Java.util.list;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.widget.gridview;import Android.widget.simpleadapter;public class Hello extends Activity {private List<map <string, integer>> list = new arraylist<map<string, integer>> ();p rivate simpleadapter Simpleadapter = null;private GridView GridView = Null;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager this.initadapter () to use, This.gridview = (GridView) Super.findviewbyid (R.id.mygridview); This.gridView.setAdapter (This.simpleadapter);} private void Initadapter () {field[] fields = R.drawable.class.getdeclaredfields ();//reflection mechanism for (int x = 0; x < Fields.leng Th X + +) {if (Fields[x].getname () startsWith ("Png_")) {map<string, integer> Map = new hashmap<string, Integer>(), try {map.put ("img", Fields[x].getint (R.drawable.class)),} catch (IllegalArgumentException e) {e.printstacktrace ( );} catch (Illegalaccessexception e) {e.printstacktrace ();} This.list.add (map);} This.simpleadapter = new Simpleadapter (this, this.list,r.layout.grid_layout, new string[] {"img"},new int[] {R.id.img} );}}}
The effect is as follows:
Android-grid view (GridView) instance