GridView的使用

  1. public class MainActivity extends Activity { 
  2.     private GridView gv_main; 
  3.  
  4.     @Override 
  5.     protected void onCreate(Bundle savedInstanceState) { 
  6.         setContentView(R.layout.main); 
  7.         super.onCreate(savedInstanceState); 
  8.         gv_main = (GridView) this.findViewById(R.id.gv_main); 
  9.         gv_main.setAdapter(new MainUIAdapter(this)); 
  10.  
  11.         gv_main.setOnItemClickListener(new OnItemClickListener() { 
  12.  
  13.             public void onItemClick(AdapterView<?> parent, View view, 
  14.                     int position, long id) { 
  15.                 switch (position) { 
  16.                 case 0// 手機防盜 
  17.                     Intent lostProtectIntent =new Intent(MainActivity.this
  18.                             LostProtectedActivity.class); 
  19.                     startActivity(lostProtectIntent); 
  20.                     break
  21.                 case 8// 設置中心 
  22.                     Intent settingIntent = new Intent(MainActivity.this
  23.                             SettingCenterActivity.class); 
  24.                     startActivity(settingIntent); 
  25.                     break
  26.                 } 
  27.  
  28.             } 
  29.         }); 
  30.     } 
  31.  

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="全山手機衛士" 
  11.         android:textColor="#66ff00" 
  12.         android:gravity="center_horizontal" 
  13.         android:textSize="28sp" > 
  14.     </TextView> 
  15.     <View 
  16.         android:paddingTop="5dip" 
  17.         android:layout_marginTop="5dip" 
  18.         android:layout_width="fill_parent" 
  19.         android:layout_height="1dip" 
  20.         android:background="@drawable/devide_line" 
  21.          > 
  22.     </View> 
  23.  
  24.     <GridView 
  25.         android:listSelector="@android:color/transparent" 
  26.         android:id="@+id/gv_main" 
  27.         android:layout_width="fill_parent" 
  28.         android:layout_height="fill_parent" 
  29.         android:numColumns="3" > 
  30.     </GridView> 
  31.  
  32. </LinearLayout> 

 

 

  1. public class MainUIAdapter extends BaseAdapter{ 
  2.      
  3.     private Context context; 
  4.     private LayoutInflater inflater; 
  5.      
  6.     public MainUIAdapter(Context context){ 
  7.          
  8.         this.context = context; 
  9.          
  10.         inflater = LayoutInflater.from(context); 
  11. //      inflater = (LayoutInflater) context 
  12. //              .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  13.      
  14.     } 
  15.      
  16.     private static final String[] names = { "手機防盜""通訊衛士""程序管理""進程管理"
  17.         "流量管理""手機殺毒""系統優化""高級工具""設置中心" }; 
  18.      
  19.     private static final int[] icons = { R.drawable.widget01, 
  20.         R.drawable.widget02, R.drawable.widget03, R.drawable.widget04, 
  21.         R.drawable.widget05, R.drawable.widget06, R.drawable.widget07, 
  22.         R.drawable.widget08, R.drawable.widget09 }; 
  23.  
  24.  
  25.     @Override 
  26.     public int getCount() { 
  27.         return names.length; 
  28.     } 
  29.  
  30.     @Override 
  31.     public Object getItem(int position) { 
  32.         return names[position]; 
  33.     } 
  34.  
  35.     @Override 
  36.     public long getItemId(int position) { 
  37.         // TODO Auto-generated method stub 
  38.         return position; 
  39.     } 
  40.  
  41.     @Override 
  42.     public View getView(int position, View convertView, ViewGroup parent) { 
  43.          
  44.         View view = inflater.inflate(R.layout.main_item, null); 
  45.         ImageView iv = (ImageView) view.findViewById(R.id.iv_main_item_icon); 
  46.         TextView tv = (TextView) view.findViewById(R.id.tv_main_item_name); 
  47.         tv.setText(names[position]); 
  48.         iv.setImageResource(icons[position]); 
  49.          
  50.         return view; 
  51.     } 
  52.  

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:gravity="center_horizontal" 
  6.     android:background="@drawable/main_item_background" 
  7.     android:orientation="vertical" > 
  8.  
  9.     <ImageView 
  10.         android:id="@+id/iv_main_item_icon" 
  11.         android:layout_width="70dip" 
  12.         android:layout_height="70dip" 
  13.         android:layout_marginTop="5dip" 
  14.         android:src="@drawable/ic_launcher" /> 
  15.      <TextView 
  16.         android:id="@+id/tv_main_item_name" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="wrap_content" 
  19.         android:layout_marginBottom="5dip" 
  20.         android:gravity="center_horizontal" 
  21.         android:text="功能名稱" 
  22.         android:textSize="18sp" /> 
  23.  
  24. </LinearLayout> 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <item android:state_pressed="true" 
  4.           android:drawable="@drawable/main_item_pressed" /> <!-- pressed --> 
  5.     <item android:state_focused="true" 
  6.           android:drawable="@drawable/main_item_pressed" /> <!-- focused --> 
  7.     <item android:drawable="@drawable/main_item_normal" /> <!-- default --> 
  8. </selector> 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"> 
  4.      
  5.     <corners android:radius="8dip"/> 
  6.     <solid android:color="#000000"></solid> 
  7.      
  8. </shape> 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"> 
  4.      
  5.     <corners android:radius="8dip"/> 
  6.     <solid android:color="#66383838"></solid> 
  7.      
  8. </shape> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章