The simple use of GridView,
Test code:
Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /></RelativeLayout>
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", margin between two rows.
Android: horizontalSpacing = "10dp", margin between two columns.
Grid_item.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" android: padding = "10dp"> <ImageView android: id = "@ + id/iamgeView" android: layout_width = "80dp" android: layout_height = "80dp" android: src = "@ drawable/ic_launcher"/> <TextView android: id = "@ + id/textView" android: layout_width = "80dp" android: layout_height = "wrap_content" android: layout_marginTop = "5dip" android: gravity = "center" android: text = "text" android: textColor = "@ android: color/holo_red_light"/> </LinearLayout>
MainActivity. java:
Package com. zzw. testgridview; 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. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; import android. widget. simpleAdapter; import android. widget. toast; public class Mai NActivity extends Activity {private GridView; private List <Map <String, Object> data; private SimpleAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init (); gridView = (GridView) findViewById (R. id. gridView); String [] from = {"image", "text"}; int [] to = {R. id. iamgeView, R. id. text View}; adapter = new SimpleAdapter (this, data, R. layout. grid_item, from, to); gridView. setAdapter (adapter); gridView. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (MainActivity. this, position + "", Toast. LENGTH_SHORT ). show () ;}}) ;}private void init () {// The image is encapsulated into an array int [] image = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e, R. drawable. f, R. drawable. g, R. drawable. h, R. drawable. i, R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e, R. drawable. f, R. drawable. g, R. drawable. h, R. drawable. i}; // The text is encapsulated into an array String [] text = new String [image. length]; for (int I = 0; I <image. length; I ++) {text [I] = "text" + I;} data = new ArrayList <Map <String, Object> (); for (int I = 0; I <image. length; I ++) {HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("image", image [I]); map. put ("text", text [I]); data. add (map );}}}