本文实例讲述了android控件之gridview用法。分享给大家供大家参考。具体如下:
gridview是一项显示二维的viewgroup,可滚动的网格。一般用来显示多张图片。
以下模拟九宫图的实现,当鼠标点击图片时会进行相应的跳转链接。
目录结构如下:
main.xml布局文件,存放gridview控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "utf-8" ?> <!-- android:numcolumns= "auto_fit" ,gridview的列数设置为自动 android:columnwidth= "90dp" ,每列的宽度,也就是item的宽度 android:stretchmode= "columnwidth" ,缩放与列宽大小同步 android:verticalspacing= "10dp" ,两行之间的边距,如:行一(no. 0 ~no. 2 )与行二(no. 3 ~no. 5 )间距为10dp android:horizontalspacing= "10dp" ,两列之间的边距 --> <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布局文件,存放显示控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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
1
2
3
4
5
6
7
8
|
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <string name= "hello" >hello world, gvactivity!</string> <string name= "app_name" >九宫图</string> <string name= "test_name1" >跳转到testactivity1</string> <string name= "test_name2" >跳转到testactivity2</string> <string name= "test_name3" >跳转到testactivity3</string> </resources> |
清单文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://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= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-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> |
跳转类testactivity1、testactivity2、testactivity3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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); } } |
类gvactivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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[]{ "宫式布局1" , "宫式布局2" , "宫式布局3" , "宫式布局4" , "宫式布局5" , "宫式布局6" , "宫式布局7" , "宫式布局8" }; gridview gridview = (gridview) findviewbyid(r.id.gridview); arraylist<hashmap<string, object>> lstimageitem = new arraylist<hashmap<string, object>>(); for ( int i = 0 ; i < 8 ; i++) { hashmap<string, object> map = new hashmap<string, object>(); map.put( "itemimage" , images[i]); map.put( "itemtext" , texts[i]); lstimageitem.add(map); } simpleadapter saimageitems = new simpleadapter( this , lstimageitem, // 数据源 r.layout.night_item, // 显示布局 new string[] { "itemimage" , "itemtext" }, new int [] { r.id.itemimage, r.id.itemtext }); gridview.setadapter(saimageitems); gridview.setonitemclicklistener( new itemclicklistener()); } class itemclicklistener implements onitemclicklistener { /** * 点击项时触发事件 * * @param parent 发生点击动作的adapterview * @param view 在adapterview中被点击的视图(它是由adapter提供的一个视图)。 * @param position 视图在adapter中的位置。 * @param rowid 被点击元素的行id。 */ public void onitemclick(adapterview<?> parent, view view, int position, long rowid) { hashmap<string, object> item = (hashmap<string, object>) parent.getitematposition(position); //获取数据源的属性值 string itemtext=(string)item.get( "itemtext" ); object object=item.get( "itemimage" ); toast.maketext(gvactivity. this , itemtext, toast.length_long).show(); //根据图片进行相应的跳转 switch (images[position]) { case r.drawable.p1: startactivity( new intent(gvactivity. this , testactivity1. class )); //启动另一个activity finish(); //结束此activity,可回收 break ; case r.drawable.p2: startactivity( new intent(gvactivity. this , testactivity2. class )); finish(); break ; case r.drawable.p3: startactivity( new intent(gvactivity. this , testactivity3. class )); finish(); break ; } } } } |
运行结果
希望本文所述对大家的android程序设计有所帮助。