天天看點

Android:GirdView實作九宮格的代碼 Android GridView屬性集合 簡單實作Android實作九宮格

看代碼前,先看一下效果吧

Android:GirdView實作九宮格的代碼 Android GridView屬性集合 簡單實作Android實作九宮格

Android GridView屬性集合

1.android:numColumns=”auto_fit”   //GridView的列數設定為自動

2.android:columnWidth=”90dp "       //每列的寬度,也就是Item的寬度

3.android:stretchMode=”columnWidth"//縮放與列寬大小同步

4.android:verticalSpacing=”10dp”          //兩行之間的邊距

5.android:horizontalSpacing=”10dp”      //兩列之間的邊距 

6.android:cacheColorHint="#00000000" //去除拖動時預設的黑色背景

7.android:listSelector="#00000000"        //去除選中時的黃色底色

8.android:scrollbars="none"                   //隐藏GridView的滾動條

9.android:fadeScrollbars="true"             //設定為true就可以實作滾動條的自動隐藏和顯示

10.android:fastScrollEnabled="true"      //GridView出現快速滾動的按鈕(至少滾動4頁才會顯示)

11.android:fadingEdge="none"                //GridView衰落(褪去)邊緣顔色為空,預設值是vertical。(可以了解為上下邊緣的提示色)

12.android:fadingEdgeLength="10dip"   //定義的衰落(褪去)邊緣的長度

13.android:stackFromBottom="true"       //設定為true時,你做好的清單就會顯示你清單的最下面

14.android:transcriptMode="alwaysScroll" //當你動态添加資料時,清單将自動往下滾動最新的條目可以自動滾動到可視範圍内

15.android:drawSelectorOnTop="false"  //點選某條記錄不放,顔色會在記錄的後面成為背景色,内容的文字可見(預設為false)

簡單實作Android實作九宮格

Android:GirdView實作九宮格的代碼 Android GridView屬性集合 簡單實作Android實作九宮格

實作的步驟

1. 一個整體的容器部分。就是上圖中包括整個圖檔項個各個部分,這裡我們使用gridView(表格布局)來實作

2.整個界面裡需要注意的是 “重複的部分”,就是 各個圖檔項和,圖檔下方顯示的文字了。那麼我們需要描述這個部分。在描述時,要說明圖檔位于上方,文字位于下方。

3.疊代,或者說重複的将各項 插入(放入)到容器内。

需要添加/修改3個檔案:main_activity,grid_item.xml,NineBox.java

Android:GirdView實作九宮格的代碼 Android GridView屬性集合 簡單實作Android實作九宮格

main_activity.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    
    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><span style="font-size:12px;">
</span>
           

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="90dp"
  android:layout_height="wrap_content"
>
   <ImageView    
               android:layout_height="wrap_content"    
               android:id="@+id/ItemImage"    
               android:layout_width="wrap_content"    
               android:layout_centerHorizontal="true">    
         </ImageView>  
         <TextView    
               android:layout_width="wrap_content"    
               android:layout_below="@+id/ItemImage"    
               android:layout_height="wrap_content"    
               android:text="TextView01"    
               android:layout_centerHorizontal="true"    
               android:id="@+id/ItemText">  
         </TextView>  
  
</RelativeLayout>
           

NineBox.java

package cn.nedu.math.ninebox;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class NineBox extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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();
      }
     }   
    }
}
           

源碼下載下傳位址:http://download.csdn.net/detail/qq_27384607/8957397