The first is two XML interfaces:
<?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"> <GridView Android:id="@+id/mygridview"Android:layout_alignparenttop="true"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:stretchmode="ColumnWidth"Android:numcolumns="3"/></linearlayout>
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="match_parent"Android:layout_height="match_parent"> <ImageView Android:id="@+id/itemimageview"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:paddingleft="10DP"android:layout_gravity="Center"/> <TextView Android:id="@+id/itemtextview"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:gravity="Center"/></linearlayout>
Then the Java implementation Program:
1 Package com.example.zhoushasha.myapplication;2 3 import android.support.v7.app.AppCompatActivity;4 import Android.os.Bundle;5 import Android.view.View;6 import Android.view.Window;7 import Android.view.WindowManager;8 import Android.widget.AdapterView;9 import Android.widget.GridView;Ten import Android.widget.SimpleAdapter; One import Android.widget.Toast; A - import java.util.ArrayList; - import Java.util.HashMap; the import java.util.List; - - Public classMainactivity extends Appcompatactivity { - PrivateGridView Mgridview; + - + A Private int[] Imageres ={r.drawable.tu1, R.DRAWABLE.TU2, at R.DRAWABLE.TU3,R.DRAWABLE.TY4,R.DRAWABLE.TU5,R.DRAWABLE.TU6,R.DRAWABLE.TU7,R.DRAWABLE.TU8,R.DRAWABLE.TU9} ; - - - - PrivateString[] ItemName = {"Figure 1","Figure 2","Figure 3","Figure 4","Figure 5", - in "Figure 6","Figure 7","Figure 8","Figure 9" }; - to @Override + - Public voidonCreate (Bundle savedinstancestate) { the * super.oncreate (savedinstancestate); $ Setcontentview (r.layout.activity_main);Panax Notoginseng - the +Mgridview =(GridView) Findviewbyid (R.id.mygridview); A thelistNewArraylist(); + - intLength =itemname.length; $ $ for(inti =0; i < length; i++) { - -hashmap<string, object> map =NewHashmap<string, object>(); the -Map.put ("Itemimageview", Imageres[i]);Wuyi theMap.put ("Itemtextview", Itemname[i]); - Wu data.add (map); - About } $ - //add an adapter for Itme.xml - -Simpleadapter Simpleadapter =NewSimpleadapter (mainactivity. This, A +Data, R.layout.item,NewString[] {"Itemimageview","Itemtextview" }, the New int[] {r.id.itemimageview,r.id.itemtextview}); - $ Mgridview.setadapter (simpleadapter); the the //Add a Click event listener for Mgridview the theMgridview.setonitemclicklistener (NewGridviewitemonclick ()); - in } the the //Defining Click event Listeners About the Public classGridviewitemonclick implements Adapterview.onitemclicklistener { the the @Override + - Public voidOnitemclick (adapterview<?> arg0, view view,intPositionLongArg3) { the BayiToast.maketext (Getapplicationcontext (), Position +"", the the toast.length_short). Show (); - - } the the } the the}
Functions can be implemented by running
Using GridView and Simpleadapter to implement the common nine Gongge of mobile interface