原文链接:http://blog.csdn.net/xyz_lmn/article/details/6906255

今天在devdiv论坛里看到有坛友问到九宫格的实现,我把我在项目中用的经验分享一下,九宫格用gridview实现代码如下:代码下载地址:http://www.devdiv.com/thread-39455-1-1.html


xml代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res/com.google.android.gx5weather"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="1.0"     android:background="@drawable/bg"     ><ImageView android:id="@+id/ImageView01"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_gravity="center_vertical"           android:background="@drawable/top"></ImageView>  <GridView xmlns:android="http://schemas.android.com/apk/res/android"       android:id="@+id/gridview"     android:layout_width="wrap_content"       android:layout_height="wrap_content"     android:numColumns="3"     android:verticalSpacing="30dip"     android:horizontalSpacing="10dip"     android:columnWidth="90dip"  //列宽    android:stretchMode="columnWidth"     android:gravity="center"    android:listSelector="@drawable/grid_selector_background"></GridView></LinearLayout>

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

public class NineBox extends Activity {    /** Called when the activity is first created. */    @Override    protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                 WindowManager.LayoutParams.FLAG_FULLSCREEN);             setContentView(R.layout.main_activity);        GridView gridview=(GridView)findViewById(R.id.gridview);        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();          for(int i=1;i<10;i++)          {            HashMap<String, Object> map = new HashMap<String, Object>();          if(i==1){                map.put("ItemImage", R.drawable.g11);                map.put("ItemText", getResources().getString(R.string.gridview1));          }          if(i==2){               map.put("ItemImage", R.drawable.g12);              map.put("ItemText", getResources().getString(R.string.gridview2));        }          if(i==3){               map.put("ItemImage", R.drawable.g13);              map.put("ItemText", getResources().getString(R.string.gridview3));        }          if(i==4){               map.put("ItemImage", R.drawable.g14);              map.put("ItemText", getResources().getString(R.string.gridview4));          }          if(i==5){               map.put("ItemImage", R.drawable.g15);              map.put("ItemText", getResources().getString(R.string.gridview5));        }          if(i==6){               map.put("ItemImage", R.drawable.g16);              map.put("ItemText", getResources().getString(R.string.gridview6));        }          if(i==7){               map.put("ItemImage", R.drawable.g17);              map.put("ItemText", getResources().getString(R.string.gridview7));        }          if(i==8){               map.put("ItemImage", R.drawable.g18);              map.put("ItemText", getResources().getString(R.string.gridview8));        }          if(i==9){               map.put("ItemImage", R.drawable.g19);              map.put("ItemText", getResources().getString(R.string.gridview9));        }          lstImageItem.add(map);                 }           SimpleAdapter saImageItems = new SimpleAdapter(this,                                                  lstImageItem,                                                  R.layout.grid_item,                                                       new String[] {"ItemImage","ItemText"},                                                     new int[] {R.id.ItemImage,R.id.ItemText});           gridview.setAdapter(saImageItems);          gridview.setOnItemClickListener(new ItemClickListener());      }         class  ItemClickListener implements OnItemClickListener      {          @SuppressWarnings("unchecked")public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened                                         View arg1,//The view within the AdapterView that was clicked                                        int arg2,//The position of the view in the adapter                                        long arg3//The row id of the item that was clicked                                      ) {             HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);           if(item.get("ItemText").equals(getResources().getString(R.string.gridview1))){      Toast.makeText(NineBox.this, R.string.gridview1, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview2))){      Toast.makeText(NineBox.this, R.string.gridview2, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview3))){      Toast.makeText(NineBox.this, R.string.gridview3, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview4))){      Toast.makeText(NineBox.this, R.string.gridview4, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview5))){      Toast.makeText(NineBox.this, R.string.gridview5, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview6))){      Toast.makeText(NineBox.this, R.string.gridview6, Toast.LENGTH_LONG).show();      }       if(item.get("ItemText").equals(getResources().getString(R.string.gridview7))){      Toast.makeText(NineBox.this, R.string.gridview7, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview8))){      Toast.makeText(NineBox.this, R.string.gridview8, Toast.LENGTH_LONG).show();      }      if(item.get("ItemText").equals(getResources().getString(R.string.gridview9))){      Toast.makeText(NineBox.this, R.string.gridview9, Toast.LENGTH_LONG).show();      }     }      }}


/**
* @author xyz_lmn
* 邮箱:xy-zhang@163.com
*
*/


更多相关文章

  1. Android单元测试 - 如何开始?
  2. Android简单调用相机Camera功能,实现打开照相功能
  3. Android(安卓)Toolchain与Bionic Libc
  4. [置顶] android 实现APK之间的跳转
  5. Android(安卓)混合了 JSON 的 Android(安卓)应用程序
  6. Android(安卓)源代码编后的目录分析
  7. Android(安卓)CountDownTimer实现定时器和倒计时效果
  8. 代码混淆
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. Android使用Fragment来实现ViewPager的功
  2. 怎么控制安卓应用的权限
  3. Android中StackOverflow的问题
  4. android中LinearGradient线性渐变
  5. Mac OS X下设置android NDK的环境(详细实
  6. Android(安卓)实现优惠卷二级list
  7. android eclipse环境搭建
  8. 使用traceview进行Android性能测试
  9. Andriod开发环境安装
  10. Android(安卓)greenDAO 数据库 简单学习