android常用控件二 gallery
2011-03-30 11:30
381 查看
gallery:
图片填充的方法与Spinner类似,也是使用setAdapter()方法。但是这个使用的是自定义的Adapter,并且这个Adapter要继承于BaseAdapter。
Gallery定义与数据填充及监听事件:
继承于BaseAdapter的ImageAdapter类,可以作为内部类来实现:
R.styleable.Gallery_android_galleryItemBackground定义于外部的attrs.xml文件中:
图片填充的方法与Spinner类似,也是使用setAdapter()方法。但是这个使用的是自定义的Adapter,并且这个Adapter要继承于BaseAdapter。
Gallery定义与数据填充及监听事件:
Gallery g = (Gallery) findViewById(R.id.mygallery); /* 添加一ImageAdapter并设置给Gallery对象 */ g.setAdapter(new ImageAdapter(this)); /* 设置一个itemclickListener并Toast被点击图片的位置 */ g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick (AdapterView<?> parent, View v, int position, long id) { Toast.makeText (EX04_10.this, getString(R.string.my_gallery_text_pre) + position+ getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show(); } });
继承于BaseAdapter的ImageAdapter类,可以作为内部类来实现:
public class ImageAdapter extends BaseAdapter { /*声明变量*/ int mGalleryItemBackground; private Context mContext; /*ImageAdapter的构造器*/ public ImageAdapter(Context c) { mContext = c; /* 使用在res/values/attrs.xml中的<declare-styleable>定义 * 的Gallery属性.*/ TypedArray a = obtainStyledAttributes(R.styleable.Gallery); /*取得Gallery属性的Index id*/ mGalleryItemBackground = a.getResourceId (R.styleable.Gallery_android_galleryItemBackground, 0); /*让对象的styleable属性能够反复使用*/ a.recycle(); } /* 覆盖的方法getCount,返回图片数目 */ public int getCount() { return myImageIds.length; } /* 覆盖的方法getItemId,返回图像的数组id */ public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } /* 覆盖的方法getView,返回一View对象 */ public View getView (int position, View convertView, ViewGroup parent) { /*产生ImageView对象*/ ImageView i = new ImageView(mContext); /*设置图片给imageView对象*/ i.setImageResource(myImageIds[position]); /*重新设置图片的宽高*/ i.setScaleType(ImageView.ScaleType.FIT_XY); /*重新设置Layout的宽高*/ i.setLayoutParams(new Gallery.LayoutParams(136, 88)); /*设置Gallery背景图*/ i.setBackgroundResource(mGalleryItemBackground); /*返回imageView对象*/ return i; } /*建构一Integer array并取得预加载Drawable的图片id*/ private Integer[] myImageIds = { R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4, R.drawable.photo5, R.drawable.photo6, }; }
R.styleable.Gallery_android_galleryItemBackground定义于外部的attrs.xml文件中:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Gallery"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
相关文章推荐
- Android第四讲——常用的基本控件(二)ListView、GridView、Spinner、AutoCompleteListView、Gallery、ExpandableListView
- Android常用控件之HorizontalScrollView代替Gallery
- Android Gallery控件使用方法详解
- Android软件开发之盘点常用系统控件界面大合集(三)
- android 常用控件--ImageView
- android常用控件属性
- Android一些常用控件
- android相对布局中控件的常用属性
- Android中的常用控件及其基本用法
- Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表
- Android常用UI控件 ViewGroup
- Android-常用UI控件(Spinner/AutoCompleteTextView)
- Android—常用控件(一)文本控件
- Android开发七:常用控件3--ListView(二)
- Android--常用控件
- 5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
- Android 常用控件屬性
- 一个封装android开发常用控件的库
- Android常用控件TextView的属性详解
- 【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)