Android Widget界面组件之网格视图GridView

移动开发
Android Widget界面组件之网格视图GridView是本文要介绍的内容,主要是来了解并学习Android Widget界面中网络视图GridView的应用。

Android Widget界面组件之网格视图GridView是本文要介绍的内容,主要是来了解并学习Android Widget界面中网络视图GridView的应用,具体内容的详解来看本文详解。

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

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

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

代码

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

小结:Android Widget界面组件之网格视图GridView的内容介绍完了,希望通过Android Widget界面内容的学习能对你有所帮助。

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-09-07 14:20:42

Android Wid组件

2011-09-08 15:51:33

Android Wid组件

2011-09-07 10:58:07

Android wid

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-08 15:40:45

Android Wid组件

2011-05-31 15:56:03

Android Gridview

2011-06-01 16:50:05

Android ListView

2011-09-13 15:35:40

Widget

2009-09-22 17:53:32

C# COM组件开发

2011-05-27 16:57:13

Android widget

2011-09-09 20:14:58

Android Wid

2011-09-08 15:07:10

Android Wid搭建

2010-07-13 09:02:19

Widget开发

2011-03-14 09:55:25

AndroidWidget

2011-09-08 11:13:29

Widget

2011-09-07 13:18:40

Android Wid

2011-09-07 17:54:40

Android Wid开发

2011-09-07 14:01:41

Android Wid实例

2010-07-23 08:54:02

2010-01-25 14:04:17

Android Wid
点赞
收藏

51CTO技术栈公众号