九宫格用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进阶之代码应用技巧
  3. android代码实现关机
  4. android 查询工具源代码
  5. Android(java)学习笔记113:Android编写代码调用Vibrator震动功能,按
  6. android 源代码错误
  7. Android sqlite 数据库在java代码中的增删改查

随机推荐

  1. Android(安卓)利用广播实现黑名单【指定
  2. Android(安卓)framework学习
  3. 关于Android弹出软键盘“顶起”View的问
  4. Android签名机制之---签名验证过程详解
  5. Android学习day01
  6. Jetpack插件化学习之AndroidX--Android项
  7. Android(安卓)ADB工具使用
  8. Android自学笔记:应用程序基本原理
  9. Android中XML解析-SAX解析
  10. Android(安卓)判断应用 第一次安装启动