The original version is implemented in the Android app
In this article, you need to add/modify three files: Main. XML, meunitem. XML, and Java.Source code.
Main. xml SourceCodeAs follows, It is a girdview, used to load items:
<? XML version = "1.0" encoding = "UTF-8"?>
<Gridview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/gridview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: numcolumns = "auto_fit"
Android: horizontalspacing = "10dp"
Android: verticalspacing = "10dp"
Android: columnwidth = "90dp"
Android: stretchmode = "columnwidth"
Android: gravity = "center"> </gridview>
Some attributes are described as follows:
Android: numcolumns = "auto_fit", set the number of columns in the gridview to automatic
Android: columnwidth = "90dp", the width of each column, that is, the width of the item
Android: stretchmode = "columnwidth", scaling and column width Synchronization
Android: verticalspacing = "10dp", the margin between two rows, for example, Row 1 (no.0 ~ No. 2) and row 2 (No. 3 ~ No. 5) 10dp spacing
Android: horizontalspacing = "10dp", margin between two columns.
Next we will introduce meunitem. XML, which is similar to the imageitem. XML in the previous listview:
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<Imageview Android: layout_width = "wrap_content"
Android: Id = "@ + ID/itemimage"
Android: layout_height = "wrap_content"
Android: layout_centerhorizontal = "true"/>
<Textview Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + ID/itemimage"
Android: Id = "@ + ID/itemtext"
Android: layout_centerhorizontal = "true"
/>
</Relativelayout>
Finally, the source code of Java
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. mainmenu );
Gridview = (gridview) findviewbyid (R. Id. gridview );
Arraylist
For (INT I = 1; I <10; I ++ ){
Hashmap <string, Object> map = new hashmap <string, Object> ();
Map. Put ("itemimage", R. drawable. Icon );
Map. Put ("itemtext", "No." + I );
Meumlist. Add (MAP );
}
Simpleadapter samenuitem = new simpleadapter (this,
Meumlist, // Data Source
R. layout. menuitem, // XML implementation
New String [] {"itemimage", "itemtext"}, // key of the corresponding map
New int [] {R. Id. itemimage, R. Id. itemtext}); // ID of the corresponding R
// Add an item to the grid
gridview. setadapter (samenuitem);
gridview. setonitemclicklistener (New onitemclicklistener () {
Public void onitemclick (adapterview Arg0, view arg1, int arg2, long arg3) {
system. out. println ("Click index:" + arg2);
}< BR >}< br> );
}< br>
attached