This example describes the GridView usage of the Android control. Share to everyone for your reference. Specifically as follows:
The GridView is a viewgroup, scrollable grid that displays two-dimensional dimensions. Typically used to display more than one picture.
The following simulation of the implementation of nine diagram, when the mouse clicks on the picture will be the corresponding jump link.
The directory structure is as follows:
Main.xml layout file, storing GridView control
<?xml version= "1.0" encoding= "Utf-8"?>
<!--
android:numcolumns= "Auto_fit", the number of columns in the GridView is set to Automatic
Android:columnwidth= "90DP", the width of each column, that is, the width of the item
android:stretchmode= "ColumnWidth", the Zoom and column size sync
android: Verticalspacing= "10DP", the margin between two lines, such as: line One (no.0~no.2) and row two (no.3~no.5) spacing is 10dp
android:horizontalspacing= "10DP", Margin between two columns
-->
<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"
/>
Night_item.xml layout files, storing display controls
<?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: Layout_width= "Wrap_content"
android:layout_centerhorizontal= "true"
android:id= "@+id/itemimage" >
</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 >
Strings.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello world, gvactivity!</string>
<string name= "App_name" > Nine map </string>
<string name= "Test_name1" > Jump to testactivity1</string>
<string name= "test_name2" > Jump to Testactivity2</string>
<string name= "Test_name3" > Jump to testactivity3</string>
</resources>
Manifest file
<?xml version= "1.0" encoding= "Utf-8"?> <manifest "xmlns:android="
Schemas.android.com/apk/res/android "package=" COM.LJQ.GV "android:versioncode=" 1 "android:versionname=" 1.0 "> <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Gvactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" and Roid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </inten t-filter> </activity> <activity android:name= ". TestActivity1 "android:label=" @string/test_name1 "/> <activity android:name=". TestActivity2 "android:label=" @string/test_name2 "/> <activity android:name=". TestActivity3 "android:label=" @string/test_name3 "/> </application> <uses-sdk android:minsdkversion=" 7 " /> </manifest>
Jump class TestActivity1, TestActivity2, TestActivity3
Package COM.LJQ.GV;
Import android.app.Activity;
Import Android.os.Bundle;
public class TestActivity1 extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
Package COM.LJQ.GV;
Import android.app.Activity;
Import Android.os.Bundle;
public class TestActivity2 extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
Package COM.LJQ.GV;
Import android.app.Activity;
Import Android.os.Bundle;
public class TestActivity3 extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
Class Gvactivity:
Package COM.LJQ.GV;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.GridView;
Import Android.widget.SimpleAdapter;
Import Android.widget.Toast;
Import Android.widget.AdapterView.OnItemClickListener;
public class Gvactivity extends activity {private String texts[] = null;
private int images[] = null;
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Images=new int[]{r.drawable.p1, R.drawable.p2, R.drawable.p3, R.DRAWABLE.P4, R.DRAWABLE.P5,R.DRAWABLE.P6,
R.DRAWABLE.P7,R.DRAWABLE.P8};
texts = new string[]{"Palace layout 1", "Palace layout 2", "Palace layout 3", "Palace layout 4", "the Palace layout 5", "the Palace layout 6", "the Palace type layout 7", "the Palace type layout 8"};
GridView GridView = (GridView) Findviewbyid (R.id.gridview); Arraylist
Run results
I hope this article will help you with your Android program.