九宫格的实现

今天在坛子里看到有坛友问到九宫格的实现,我把我在项目中用的经验分享一下,九宫格用gridview实现代码如下:

xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <ImageView android: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. <GridView xmlns: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"   //九宫格的背景,可以找个圆角正方形

  1. public class NineBox extends Activity {
  2.     /** Called when the activity is first created. */
  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState) {
  5.                 // TODO Auto-generated method stub
  6.                 super.onCreate(savedInstanceState);
  7.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  8.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
  9.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  10.      
  11.         setContentView(R.layout.main_activity);
  12.         GridView gridview=(GridView)findViewById(R.id.gridview);
  13.         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
  14.         for(int i=1;i<10;i++)  
  15.         {  
  16.           HashMap<String, Object> map = new HashMap<String, Object>();
  17.           if(i==1){
  18.                 map.put("ItemImage", R.drawable.g11);
  19.                 map.put("ItemText", getResources().getString(R.string.gridview1));
  20.           }
  21.           if(i==2){ 
  22.               map.put("ItemImage", R.drawable.g12);
  23.               map.put("ItemText", getResources().getString(R.string.gridview2));
  24.         }
  25.           if(i==3){ 
  26.               map.put("ItemImage", R.drawable.g13);
  27.               map.put("ItemText", getResources().getString(R.string.gridview3));
  28.         }
  29.           if(i==4){ 
  30.               map.put("ItemImage", R.drawable.g14);
  31.               map.put("ItemText", getResources().getString(R.string.gridview4));  
  32.         }
  33.           if(i==5){ 
  34.               map.put("ItemImage", R.drawable.g15);
  35.               map.put("ItemText", getResources().getString(R.string.gridview5));
  36.         }
  37.           if(i==6){ 
  38.               map.put("ItemImage", R.drawable.g16);
  39.               map.put("ItemText", getResources().getString(R.string.gridview6));
  40.         }
  41.           if(i==7){ 
  42.               map.put("ItemImage", R.drawable.g17);
  43.               map.put("ItemText", getResources().getString(R.string.gridview7));
  44.         }
  45.           if(i==8){ 
  46.               map.put("ItemImage", R.drawable.g18);
  47.               map.put("ItemText", getResources().getString(R.string.gridview8));
  48.         }
  49.           if(i==9){ 
  50.               map.put("ItemImage", R.drawable.g19);
  51.               map.put("ItemText", getResources().getString(R.string.gridview9));
  52.         }
  53.           lstImageItem.add(map);
  54.          
  55.         }  

  56.         SimpleAdapter saImageItems = new SimpleAdapter(this,
  57.                                                   lstImageItem,
  58.                                                   R.layout.grid_item,     
  59.                                                   new String[] {"ItemImage","ItemText"},   
  60.                                                   new int[] {R.id.ItemImage,R.id.ItemText});  
  61.  
  62.         gridview.setAdapter(saImageItems);  
  63.         gridview.setOnItemClickListener(new ItemClickListener());  
  64.     }  
  65.       
  66.  
  67.         class  ItemClickListener implements OnItemClickListener  
  68.     {  

  69.        @SuppressWarnings("unchecked")
  70.         public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
  71.                                       View arg1,//The view within the AdapterView that was clicked  
  72.                                       int arg2,//The position of the view in the adapter  
  73.                                       long arg3//The row id of the item that was clicked  
  74.                                     ) {  
  75.      
  76.       HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);  
  77.    
  78.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview1))){
  79.               Toast.makeText(NineBox.this, R.string.gridview1, Toast.LENGTH_LONG).show();
  80.       }
  81.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview2))){
  82.               Toast.makeText(NineBox.this, R.string.gridview2, Toast.LENGTH_LONG).show();
  83.       }
  84.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview3))){
  85.               Toast.makeText(NineBox.this, R.string.gridview3, Toast.LENGTH_LONG).show();
  86.       }
  87.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview4))){
  88.               Toast.makeText(NineBox.this, R.string.gridview4, Toast.LENGTH_LONG).show();
  89.       }
  90.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview5))){
  91.               Toast.makeText(NineBox.this, R.string.gridview5, Toast.LENGTH_LONG).show();
  92.       }
  93.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview6))){
  94.               Toast.makeText(NineBox.this, R.string.gridview6, Toast.LENGTH_LONG).show();
  95.       }

  96.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview7))){
  97.               Toast.makeText(NineBox.this, R.string.gridview7, Toast.LENGTH_LONG).show();
  98.       }
  99.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview8))){
  100.               Toast.makeText(NineBox.this, R.string.gridview8, Toast.LENGTH_LONG).show();
  101.       }
  102.       if(item.get("ItemText").equals(getResources().getString(R.string.gridview9))){
  103.               Toast.makeText(NineBox.this, R.string.gridview9, Toast.LENGTH_LONG).show();
  104.       }
  105.      }  
  106.     }
  107. }
复制代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值