Both the GridView and ListView are commonly used multi-control la S, and the GridView is the first choice to implement the jiugong diagram! This article describes how to use the GridView to implement the jiugong diagram. The most widely used method on the Internet is to implement an ImageAdapter to inherit the BaseAdapter and use it for the GridView. This method is not repeated in this article, the usage of the GridView described in this article is very similar to that of the previous ListView. I am also a little lazy ....
First, paste the code running result of this article:
In this article, you need to add/modify three files: main. xml, night_item.xml, and JAVA source code.
The source code of main. xml is as follows, which is a GirdView, used to load items:Copy codeThe Code is as follows: <? 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: verticalSpacing = "10dp"
Android: horizontalSpacing = "10dp"
Android: columnWidth = "90dp"
Android: stretchMode = "columnWidth"
Android: gravity = "center"
/>
<? 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: verticalSpacing = "10dp"
Android: horizontalSpacing = "10dp"
Android: columnWidth = "90dp"
Android: stretchMode = "columnWidth"
Android: gravity = "center"
/>
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 the content of "maid", which is similar to the ImageItem. XML of the previous ListView:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_height = "wrap_content"
Android: paddingBottom = "4dip" android: layout_width = "fill_parent">
<ImageView
Android: layout_height = "wrap_content"
Android: id = "@ + id/ItemImage"
Android: layout_width = "wrap_content"
Android: layout_centerHorizontal = "true">
</ImageView>
<TextView
Android: layout_width = "wrap_content"
Android: layout_below = "@ + id/ItemImage"
Android: layout_height = "wrap_content"
Android: text = "TextView01"
Android: layout_centerHorizontal = "true"
Android: id = "@ + id/ItemText">
</TextView>
</RelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_height = "wrap_content"
Android: paddingBottom = "4dip" android: layout_width = "fill_parent">
<ImageView
Android: layout_height = "wrap_content"
Android: id = "@ + id/ItemImage"
Android: layout_width = "wrap_content"
Android: layout_centerHorizontal = "true">
</ImageView>
<TextView
Android: layout_width = "wrap_content"
Android: layout_below = "@ + id/ItemImage"
Android: layout_height = "wrap_content"
Android: text = "TextView01"
Android: layout_centerHorizontal = "true"
Android: id = "@ + id/ItemText">
</TextView>
</RelativeLayout>
The final part is the source code of JAVA, which is similar to the JAVA source code of the previous ListView, but the "selected" event processing is added:Copy codeThe Code is as follows: view plaincopy to clipboardprint?
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
GridView gridview = (GridView) findViewById (R. id. gridview );
// Generate dynamic array and transfer data
ArrayList <HashMap <String, Object> lstImageItem = new ArrayList <HashMap <String, Object> ();
For (int I = 0; I <10; I ++)