码迷,mamicode.com
首页 > 移动开发 > 详细

安卓学习————安卓控件之GridView

时间:2015-04-10 00:59:29      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

今天写了一个简单的GridView,它是格子视图,可以将很多图片以一定大小显示出来在做相册、图片浏览是非常有用一下是xml文件:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <GridView android:id="@+id/gridview"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:numColumns="auto_fit"
11         android:verticalSpacing="10dp"
12         android:horizontalSpacing="10dp"
13         android:columnWidth="90dp"
14         android:stretchMode="columnWidth"
15         android:gravity="center">
16         
17     </GridView>
18 </LinearLayout>
 1 package com.example.widget;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.widget.BaseAdapter;
10 import android.widget.GridView;
11 import android.widget.ImageView;
12 
13 public class GridViewActivity extends Activity {
14 
15     private GridView gridview;
16     private int[] mThumbIds = { R.drawable.acmilan, R.drawable.fcb,
17             R.drawable.manutd, R.drawable.rmcdf };
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         // TODO Auto-generated method stub
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.gridview);
24 
25         gridview = (GridView) findViewById(R.id.gridview);
26         gridview.setAdapter(new ImageAdapter(this));
27     }
28 
29     public class ImageAdapter extends BaseAdapter {
30 
31         private Context mContext;
32 
33         public ImageAdapter(Context c) {
34             mContext = c;
35         }
36 
37         @Override
38         public int getCount() {
39             // TODO Auto-generated method stub
40             return mThumbIds.length;
41         }
42 
43         @Override
44         public Object getItem(int position) {
45             // TODO Auto-generated method stub
46             return null;
47         }
48 
49         @Override
50         public long getItemId(int position) {
51             // TODO Auto-generated method stub
52             return 0;
53         }
54 
55         @Override
56         public View getView(int position, View convertView,
57                 ViewGroup parent) {
58             // TODO Auto-generated method stub
59             ImageView imageView;
60 
61             if (convertView == null) {
62                 imageView = new ImageView(mContext);
63                 imageView.setLayoutParams(new GridView.LayoutParams(
64                         85, 85));
65                 imageView
66                         .setScaleType(ImageView.ScaleType.FIT_CENTER);
67                 imageView.setPadding(8, 8, 8, 8);
68             } else {
69                 imageView = (ImageView) convertView;
70             }
71 
72             imageView.setImageResource(mThumbIds[position]);
73             return imageView;
74         }
75 
76     }
77 
78     @Override
79     public boolean onCreateOptionsMenu(Menu menu) {
80         // TODO Auto-generated method stub
81         return super.onCreateOptionsMenu(menu);
82     }
83 
84 }

 

  这个例子最关键的地方就是ImageAdapter,它是继承自BashAdapter的类里面的方法都是继承的相应接口,getview函数是关键,他负责将获取数据后的组件返回,然后再讲gridview添加上适配器,这是一个简单的小例子,主要是熟悉一下BashAdapter的使用。

安卓学习————安卓控件之GridView

标签:

原文地址:http://www.cnblogs.com/selfimprovement/p/4412952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!