九宫格的实现及九宫格源码

原创
2013/07/30 14:39
阅读数 456

九宫格用gridview实现代码如下:

XML/HTML代码

1 <?xmlversion="1.0"encoding="utf-8"?>

2 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

3 xmlns:app="http://schemas.android.com/apk/res/com.google.android.gx5weather"

4 android:orientation="vertical"

5 android:layout_width="fill_parent"

6 android:layout_height="fill_parent"

7 android:layout_weight="1.0"

8 android:background="@drawable/bg"

9 >

10 <ImageViewandroid:id="@+id/ImageView01"

11 android:layout_width="wrap_content"

12 android:layout_height="wrap_content"

13 android:layout_gravity="center_vertical"

14 android:background="@drawable/top"></ImageView>

15 <GridViewxmlns:android="http://schemas.android.com/apk/res/android"

16 android:id="@+id/gridview"

17 android:layout_width="wrap_content"

18 android:layout_height="wrap_content"

19 android:numColumns="3"

20 android:verticalSpacing="30dip"

21 android:horizontalSpacing="10dip"

22 android:columnWidth="90dip"//列宽

23 android:stretchMode="columnWidth"

24 android:gravity="center"

25 android:listSelector="@drawable/grid_selector_background"

26 >

27 </GridView>

28 </LinearLayout>


Android:numColumns="3" //九宫格的列数 auto_fit时为自动 android:listSelector="@drawable/grid_selector_background" //九宫格的背景,可以找个圆角正方形

Java代码

29 publicclassNineBoxextendsActivity{

30 /**Calledwhentheactivityisfirstcreated.*/

31 @Override

32 protectedvoidonCreate(BundlesavedInstanceState){

33 //TODOAuto-generatedmethodstub

34 super.onCreate(savedInstanceState);

35 this.requestWindowFeature(Window.FEATURE_NO_TITLE);

36 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

37 WindowManager.LayoutParams.FLAG_FULLSCREEN);

38 

39 setContentView(R.layout.main_activity);

40 GridViewgridview=(GridView)findViewById(R.id.gridview);

41 ArrayList<HashMap<String,Object>>lstImageItem=newArrayList<HashMap<String,Object>>();

42 for(inti=1;i<10;i++)

43 {

44 HashMap<String,Object>map=newHashMap<String,Object>();

45 if(i==1){

46 map.put("ItemImage",R.drawable.g11);

47 map.put("ItemText",getResources().getString(R.string.gridview1));

48 }

49 if(i==2){

50 map.put("ItemImage",R.drawable.g12);

51 map.put("ItemText",getResources().getString(R.string.gridview2));

52 }

53 if(i==3){

54 map.put("ItemImage",R.drawable.g13);

55 map.put("ItemText",getResources().getString(R.string.gridview3));

56 }

57 if(i==4){

58 map.put("ItemImage",R.drawable.g14);

59 map.put("ItemText",getResources().getString(R.string.gridview4));

60 }

61 if(i==5){

62 map.put("ItemImage",R.drawable.g15);

63 map.put("ItemText",getResources().getString(R.string.gridview5));

64 }

65 if(i==6){

66 map.put("ItemImage",R.drawable.g16);

67 map.put("ItemText",getResources().getString(R.string.gridview6));

68 }

69 if(i==7){

70 map.put("ItemImage",R.drawable.g17);

71 map.put("ItemText",getResources().getString(R.string.gridview7));

72 }

73 if(i==8){

74 map.put("ItemImage",R.drawable.g18);

75 map.put("ItemText",getResources().getString(R.string.gridview8));

76 }

77 if(i==9){

78 map.put("ItemImage",R.drawable.g19);

79 map.put("ItemText",getResources().getString(R.string.gridview9));

80 }

81 lstImageItem.add(map);

82 

83 }

84 

85 SimpleAdaptersaImageItems=newSimpleAdapter(this,

86 lstImageItem,

87 R.layout.grid_item,

88 newString[]{"ItemImage","ItemText"},

89 newint[]{R.id.ItemImage,R.id.ItemText});

90 

91 gridview.setAdapter(saImageItems);

92 gridview.setOnItemClickListener(newItemClickListener());

93 }

94 

95 

96 classItemClickListenerimplementsOnItemClickListener

97 {

98 

99 @SuppressWarnings("unchecked")

100 publicvoidonItemClick(AdapterView<?>arg0,//TheAdapterViewwheretheclickhappened

101 Viewarg1,//TheviewwithintheAdapterViewthatwasclicked

102 intarg2,//Thepositionoftheviewintheadapter

103 longarg3//Therowidoftheitemthatwasclicked

104 ){

105 

106 HashMap<String,Object>item=(HashMap<String,Object>)arg0.getItemAtPosition(arg2);

107 

108 if(item.get("ItemText").equals(getResources().getString(R.string.gridview1))){

109 Toast.makeText(NineBox.this,R.string.gridview1,Toast.LENGTH_LONG).show();

110 }

111 if(item.get("ItemText").equals(getResources().getString(R.string.gridview2))){

112 Toast.makeText(NineBox.this,R.string.gridview2,Toast.LENGTH_LONG).show();

113 }

114 if(item.get("ItemText").equals(getResources().getString(R.string.gridview3))){

115 Toast.makeText(NineBox.this,R.string.gridview3,Toast.LENGTH_LONG).show();

116 }

117 if(item.get("ItemText").equals(getResources().getString(R.string.gridview4))){

118 Toast.makeText(NineBox.this,R.string.gridview4,Toast.LENGTH_LONG).show();

119 }

120 if(item.get("ItemText").equals(getResources().getString(R.string.gridview5))){

121 Toast.makeText(NineBox.this,R.string.gridview5,Toast.LENGTH_LONG).show();

122 }

123 if(item.get("ItemText").equals(getResources().getString(R.string.gridview6))){

124 Toast.makeText(NineBox.this,R.string.gridview6,Toast.LENGTH_LONG).show();

125 }

126 

127 if(item.get("ItemText").equals(getResources().getString(R.string.gridview7))){

128 Toast.makeText(NineBox.this,R.string.gridview7,Toast.LENGTH_LONG).show();

129 }

130 if(item.get("ItemText").equals(getResources().getString(R.string.gridview8))){

131 Toast.makeText(NineBox.this,R.string.gridview8,Toast.LENGTH_LONG).show();

132 }

133 if(item.get("ItemText").equals(getResources().getString(R.string.gridview9))){

134 Toast.makeText(NineBox.this,R.string.gridview9,Toast.LENGTH_LONG).show();

135 }

136 }

137 }

138 }


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部