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

XML/HTML代码

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

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

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

4android:orientation="vertical"

5android:layout_width="fill_parent"

6android:layout_height="fill_parent"

7android:layout_weight="1.0"

8android:background="@drawable/bg"

9>

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

11android:layout_width="wrap_content"

12android:layout_height="wrap_content"

13android:layout_gravity="center_vertical"

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

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

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

17android:layout_width="wrap_content"

18android:layout_height="wrap_content"

19android:numColumns="3"

20android:verticalSpacing="30dip"

21android:horizontalSpacing="10dip"

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

23android:stretchMode="columnWidth"

24android:gravity="center"

25android:listSelector="@drawable/grid_selector_background"

26>

27</GridView>

28</LinearLayout>


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

Java代码

29publicclassNineBoxextendsActivity{

30/**Calledwhentheactivityisfirstcreated.*/

31@Override

32protectedvoidonCreate(BundlesavedInstanceState){

33//TODOAuto-generatedmethodstub

34super.onCreate(savedInstanceState);

35this.requestWindowFeature(Window.FEATURE_NO_TITLE);

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

37WindowManager.LayoutParams.FLAG_FULLSCREEN);

38

39setContentView(R.layout.main_activity);

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

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

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

43{

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

45if(i==1){

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

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

48}

49if(i==2){

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

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

52}

53if(i==3){

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

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

56}

57if(i==4){

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

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

60}

61if(i==5){

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

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

64}

65if(i==6){

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

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

68}

69if(i==7){

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

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

72}

73if(i==8){

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

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

76}

77if(i==9){

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

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

80}

81lstImageItem.add(map);

82

83}

84

85SimpleAdaptersaImageItems=newSimpleAdapter(this,

86lstImageItem,

87R.layout.grid_item,

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

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

90

91gridview.setAdapter(saImageItems);

92gridview.setOnItemClickListener(newItemClickListener());

93}

94

95

96classItemClickListenerimplementsOnItemClickListener

97{

98

99@SuppressWarnings("unchecked")

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

101Viewarg1,//TheviewwithintheAdapterViewthatwasclicked

102intarg2,//Thepositionoftheviewintheadapter

103longarg3//Therowidoftheitemthatwasclicked

104){

105

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

107

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

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

110}

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

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

113}

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

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

116}

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

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

119}

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

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

122}

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

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

125}

126

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

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

129}

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

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

132}

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

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

135}

136}

137}

138}


更多相关文章

  1. Android中的界面布局之帧布局,相对布局
  2. Android中AlertDialog用法实例分析
  3. Android(安卓)五大布局
  4. Android(安卓)亲测源码分享
  5. android 设置进入全屏,取消全屏的几种方式
  6. layout中设置图片自适应大小,并且设置最大宽高
  7. Android(安卓)代码修改按钮上的图片
  8. Android消息机制不完全解析(下)
  9. Android(安卓)使用弹出对话框,报Unable to add window错误

随机推荐

  1. Android(安卓)bluetooth start discovery
  2. RxJava在Android的应用场景
  3. Android(安卓)7.1 导航栏增加按键
  4. android dialog使用小结
  5. Android(安卓)- match_parent 和 fill_pa
  6. android:onClick attribute
  7. Writing an Android Sync Provider
  8. Android Cursor遍历
  9. pull解析和json编码
  10. appium启动APP配置参数: