package xiaosi.gallery;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 GalleryActivity extends Activity {    /** Called when the activity is first created. */    private Gallery gallery =null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                gallery = (Gallery)findViewById(R.id.gallery);        //设置图片适配器        gallery.setAdapter(new ImageAdapter(this));        gallery.setSpacing(5);                //设置监听器        gallery.setOnItemClickListener(new OnItemClickListener() {                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    long arg3) {                Toast.makeText(GalleryActivity.this, "点击了第"+arg2+"张图片", Toast.LENGTH_LONG).show();            }        });    }}class ImageAdapter extends BaseAdapter{int mGalleryItemBackground;    private Context context;    //图片源数组    private Integer[] imageInteger={            R.drawable.a,            R.drawable.b,            R.drawable.c,            R.drawable.d    };    public ImageAdapter(Context c){        context = c;        TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);                mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);                attr.recycle();    }    // 获取图片的个数    public int getCount() {        return imageInteger.length;    }    // 获取图片在库中的位置      public Object getItem(int position) {        return position;    }    // 获取图片ID      public long getItemId(int position) {        return position;    }    public View getView(int position, View convertView, ViewGroup parent) {            ImageView imageView = new ImageView(context);        // 给ImageView设置资源        imageView.setImageResource(imageInteger[position]);        // 设置显示比例类型          imageView.setScaleType(ImageView.ScaleType.FIT_XY);        // 设置布局 图片120*80         imageView.setLayoutParams(new Gallery.LayoutParams(180, 100));        imageView.setBackgroundResource(mGalleryItemBackground);                return imageView;    }}


mian.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><Gallery    android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="bottom"        android:background="?android:galleryItemBackground"/></LinearLayout>


创建一个新的XML文件在 res/values/目录下 attrs.xml命名。

<?xml version="1.0" encoding="utf-8"?><resources>        <declare-styleable name="HelloGallery">                <attr name="android:galleryItemBackground" />        </declare-styleable></resources>


这是一个定制的styleable资源,可以应用于一个布局。

作者:SJF0115 发表于2012-2-12 22:40:49 原文链接 阅读:5 评论:0 查看评论

更多相关文章

  1. android ScrollView和ListView固定底部
  2. android_camera_003
  3. Android(安卓)获取音频焦点工具类
  4. Android(安卓)在自定义view中动态设置布局规则
  5. WebView的使用
  6. android读取手机sd卡单个图片,绝对路径
  7. 访问网络的Json和图片
  8. android 获取屏幕宽高 & 控件宽高
  9. 【Android】时间与日期Widget(DatePicker 与 TimePicker)

随机推荐

  1. Android 键盘挤压 按钮
  2. Android支付宝支付详解
  3. android显示键盘时的页面自动调整
  4. Context 详解
  5. 天天记录 - Android创建应用窗口分析2
  6. Android MVP模式中的OOM
  7. NDK开发指南---NDK安装
  8. [置顶] Android多点触控揭秘
  9. android开发每日汇总【2011-10-17】
  10. 深入浅出Android事件分发机制——源码分