Android 控件之十二:Gallery画廊
来源:互联网 发布:沙漠之鹰数据图 编辑:程序博客网 时间:2024/06/05 12:49
import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;public class MainHelloGallery extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 定义UI组件final ImageView iv = (ImageView) findViewById(R.id.ImageView01);Gallery g = (Gallery) findViewById(R.id.Gallery01);// 设置图片匹配器g.setAdapter(new ImageAdapter(this));// 设置AdapterView点击监听器,Gallery是AdapterView的子类g.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 显示点击的是第几张图片Toast.makeText(MainHelloGallery.this, "" + position,Toast.LENGTH_LONG).show();// 设置背景部分的ImageView显示当前Item的图片iv.setImageResource(((ImageView) view).getId());}});}// 定义继承BaseAdapter的匹配器public class ImageAdapter extends BaseAdapter {// Item的修饰背景int mGalleryItemBackground;// 上下文对象private Context mContext;// 图片数组private Integer[] mImageIds = { R.drawable.wallpaper_0,R.drawable.wallpaper_1, R.drawable.wallpaper_2,R.drawable.wallpaper_3, R.drawable.wallpaper_4,R.drawable.wallpaper_5, R.drawable.wallpaper_6,R.drawable.wallpaper_7, R.drawable.wallpaper_8,R.drawable.wallpaper_9 };// 构造方法public ImageAdapter(Context c) {mContext = c;// 读取styleable资源TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}// 返回项目数量@Overridepublic int getCount() {return mImageIds.length;}// 返回项目@Overridepublic Object getItem(int position) {return position;}// 返回项目Id@Overridepublic long getItemId(int position) {return position;}// 返回视图@Overridepublic View getView(int position, View convertVieww, ViewGroup parent) {ImageView iv = new ImageView(mContext);iv.setImageResource(mImageIds[position]);// 给生成的ImageView设置Id,不设置的话Id都是-1iv.setId(mImageIds[position]);iv.setLayoutParams(new Gallery.LayoutParams(1200, 160));iv.setScaleType(ImageView.ScaleType.FIT_XY);iv.setBackgroundResource(mGalleryItemBackgroundd);return iv;}}}
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/ImageView01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/wallpaper_0" > </ImageView> <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="5dp" > </Gallery></FrameLayout>
在 res/values/目录中新建一个 attrs.xml内容如下:<?xml version="1.0" encoding="UTF-8"?><resources> <declare-styleable name="HelloGallery" > <attr name="android:galleryItemBackground" > </attr> </declare-styleable></resources>
0 0
- Android 控件之十二:Gallery画廊
- Android控件之Gallery(画廊)
- Android基础--Gallery画廊控件
- android gallery 画廊控件示例
- 【Android 开发】:UI控件之 Gallery 画廊控件的使用
- android之路Gallery 画廊
- 高级控件之画廊视图(Gallery)
- Android UI控件详解-Gallery(画廊)
- Android Gallery画廊 图片展示控件
- android 画廊控件Gallery的使用
- gallery画廊控件
- android开发之gallery(画廊)
- android 画廊 gallery
- Android--Gallery,画廊
- Android---画廊(Gallery)
- Android gallery画廊
- Android中的画廊(Gallery)
- Android Gallery画廊
- OC语言NSDate和NSFormater
- MySQL的基础命令
- Axure学习之流程图组件和自定义部件
- P-Associated-URI处理流程
- OC 基本知识点总结
- Android 控件之十二:Gallery画廊
- VMware ESX与VMware ESXi区别
- 使用线程的几种方式(三)客户/服务器
- IOS--UIButton的使用方法详细
- 在字符串中找子串
- IOS--UIAlertView的使用方法详细
- hdu 4289 Control【最小割+拆点】
- 浙江大学PAT_甲级_1062. Talent and Virtue (25)
- NBUT 1223 Friends number