android用户界面-组件Widget-网格视图GridView

GridView按照行列的方式来显示内容。一般适合显示图标。图片等内容,主要用于设置Adapter。

通过基础类BaseAdapter类,重写getView()方法来设置图片显示格式。

 

/Chapter04_UI_GridView01/src/com/amaker/test/MainActivity.java

 


  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.GridView;  
  12. import android.widget.ImageView;  
  13.  
  14. public class MainActivity extends Activity {  
  15.     private GridView gv;  
  16.       
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         gv = (GridView)findViewById(R.id.GridView01);  
  22.         gv.setNumColumns(4);  
  23.         // gv.setNumColumns(3);  
  24.         // String[] strs = {"a","a1","a2","b","b1","b2","c","c1","c2"};  
  25.         // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item,strs);  
  26.         gv.setAdapter(new MyAdapter(this));  
  27.           
  28.     }  
  29.       
  30.     class MyAdapter extends BaseAdapter{  
  31.           
  32.         private Integer[] imgs = {  
  33.                 R.drawable.gallery_photo_1,  
  34.                 R.drawable.gallery_photo_2,  
  35.                 R.drawable.gallery_photo_3,  
  36.                 R.drawable.gallery_photo_4,  
  37.                 R.drawable.gallery_photo_5,  
  38.                 R.drawable.gallery_photo_6,  
  39.                 R.drawable.gallery_photo_7,  
  40.                 R.drawable.gallery_photo_8,  
  41.                   
  42.                 R.drawable.gallery_photo_1,  
  43.                 R.drawable.gallery_photo_2,  
  44.                 R.drawable.gallery_photo_3,  
  45.                 R.drawable.gallery_photo_4,  
  46.                 R.drawable.gallery_photo_5,  
  47.                 R.drawable.gallery_photo_6,  
  48.                 R.drawable.gallery_photo_7,  
  49.                 R.drawable.gallery_photo_8  
  50.         };  
  51.         Context context;  
  52.         MyAdapter(Context context){  
  53.             this.context = context;  
  54.         }  
  55.         public int getCount() {  
  56.             return imgs.length;  
  57.         }  
  58.  
  59.         public Object getItem(int item) {  
  60.             return item;  
  61.         }  
  62.  
  63.         public long getItemId(int id) {  
  64.             return id;  
  65.         }  
  66.  
  67.         public View getView(int position, View convertView, ViewGroup parent) {  
  68.              ImageView imageView;  
  69.                 if (convertView == null) {  
  70.                     imageView = new ImageView(context);  
  71.                     imageView.setLayoutParams(new GridView.LayoutParams(4545));  
  72.                     imageView.setAdjustViewBounds(false);  
  73.                     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  74.                     imageView.setPadding(8888);  
  75.                 } else {  
  76.                     imageView = (ImageView) convertView;  
  77.                 }  
  78.                 imageView.setImageResource(imgs[position]);  
  79.                 return imageView;  
  80.         }  
  81.     }  

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080690

上一篇:CentOS 6.5安装部署Glusterfs


下一篇:关于jsp和servlet中实现页面跳转的几种方式总结