Some of the properties of Girdview:
android:numcolumns= "Auto_fit"--------number of columns set to Automatic
Android:columnwidth= "90DP",----------the width of each column, which is the width of item
Android:stretchmode= "columnWidth"------zoom is synchronized with column width size
android:verticalspacing= "10DP"----------vertical margins
android:horizontalspacing= "10DP"-------Horizontal margins
1. Prepare the data source
2. New Adapter
3. Load Adapter
GridView (grid view) is the way to display the contents of the column, generally used to display pictures, pictures and other content, such as the implementation of nine Bongtu, with the GridView is the first choice, but also the simplest, the following example,
Download Examples >>>
:
Mainactivity.java
PackageCom.example.testgridview;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.GridView;ImportAndroid.widget.SimpleAdapter; Public classMainactivityextendsActivity {PrivateGridView Gview; PrivateList<map<string, object>>data_list; PrivateSimpleadapter Sim_adapter; //picture encapsulated as an array Private int[] icon ={r.drawable.address_book, R.drawable.calendar, R.drawable.camera, R.drawable.clock, R.drawable.games_cont Rol, R.drawable.messenger, R.drawable.ringtone, R.drawable.settings, R.drawable.speech_balloon, R.dr Awable.weather, R.drawable.world, r.drawable.youtube}; PrivateString[] Iconname = {"Contacts", "Calendar", "Camera", "clock", "Game", "SMS", "Ringtone", "Settings", "Voice", "Weather", "browser", "video" }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.test); Gview=(GridView) Findviewbyid (R.id.gview); //New ListData_list =NewArraylist<map<string, object>>(); //Get DataGetData (); //New AdapterString [] from ={"image", "text"}; int[] to ={R.id.image,r.id.text}; Sim_adapter=NewSimpleadapter ( This, Data_list, R.layout.item, from, to); //Configure the adapterGview.setadapter (Sim_adapter); } PublicList<map<string, object>>GetData () {//the lengths of Cion and Iconname are the same, and any one of them can be for(inti=0;i<icon.length;i++) {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Image", Icon[i]); Map.put ("Text", Iconname[i]); Data_list.add (map); } returndata_list; } }
Test.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#000" > <GridViewAndroid:id= "@+id/gview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:numcolumns= "Auto_fit"Android:columnwidth= "80DP"Android:stretchmode= "ColumnWidth" ></GridView></LinearLayout>
Item.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical"android:gravity= "Center"android:padding= "10DP" > <ImageViewandroid:src= "@drawable/ic_launcher"Android:id= "@+id/image"Android:layout_width= "60DP"Android:layout_height= "60DP" /><TextViewAndroid:id= "@+id/text"Android:layout_margintop= "5DP"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#ffffff"Android:text= "text" /></LinearLayout>