Android gridview mesh, multi-row and multi-column implementation, Android gridview
Main Activity ()
Private int [] image = {R. drawable. camera, R. drawable. wifi, R. drawable. temperature, R. drawable. lamp, R. drawable. wechat, R. drawable. mic,}; private String [] text = {"camera", "network", "Temperature and Humidity", "electric appliance", "", "Voice "};
// Display the interface of the GridView = (gridview) findViewById (R. id. gridView1); ArrayList <HashMap <String, Object> imagelist = new ArrayList <HashMap <String, Object> (); // use HashMap to add an image to an array, note that it must be of the HashMap <String, Object> type, because the image loaded into the map is a resource ID, not the image itself // if it is of the findViewById (R. drawable. image) in this way, the real image is taken out and put into the map. for (int I = 0; I <6; I ++) {HashMap <String, object> map = new HashMap <String, Object> (); map. put ("image", image [I]); map. put ("text", text [I]); imagelist. add (map);} http://www.cnblogs.com/xiaobo-Linux/ qq463431476 SimpleAdapter simpleAdapter = new SimpleAdapter (this, imagelist, R. layout. items, new String [] {"image", "text"}, new int [] {R. id. image, R. id. title}); // set the GridView adapter to the new simpleAdapter gridview. setAdapter (simpleAdapter );
Master xml
<GridLayout android:layout_width="match_parent" android:layout_height="83dp" android:columnCount="1" > </GridLayout> <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="53dp" android:layout_gravity="left|bottom" android:columnWidth="90dp" android:fadeScrollbars="true" android:numColumns="2" android:stretchMode="columnWidth" />
Items. xml
<?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:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10px" android:scaleType="fitCenter" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="5px" /></LinearLayout>
Full list of GridView attributes in Android
Android: numColumns = "auto_fit" // set the columns in the GridView to auto 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 the two rows android: horizontalSpacing = "10dp" // the margin between the two columns android: cacheColorHint = "#00000000" // remove the default black background android when dragging: listSelector = "#00000000" // remove the selected yellow background color android: scrollbars = "none" // hide the GridView scroll bar android: fadeScrollbars = "true" // set it to t Rue can automatically hide and display the scroll bar. android: fastScrollEnabled = "true" // The Quick scroll button appears in the GridView (at least four pages will be displayed). android: fadingEdge = "none" // the color of the fading edge of the GridView is null. The default value is vertical. (It can be understood as the color of the upper and lower edges.) android: fadingEdgeLength = "10dip" // defines the length of the fading edges. android: stackFromBottom = "true" // when it is set to true, the list you have prepared will display the bottom android: transcriptMode = "alwaysScroll" // when you add data dynamically, the list will automatically scroll down the latest entries to automatically scroll to the visible range android: drawselectid Top = "false" // click a record, the color will become the background color after the record, the text of the content is visible (default value: false)