1,创建配置文件

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

2,编写适配器,因为Gallery 需要一个适配器填充,而且填充的必须输图片,所以。。。

package com.kang.fei.gallery;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {private Context context;int mGalleryItemBackground;private Integer[] mImageids ={R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6};public ImageAdapter(Context context){this.context = context;//TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到TypedArray a = context.obtainStyledAttributes(R.styleable.HelloGallery);mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}public int getCount() {return mImageids.length;}public Object getItem(int position) {return position;}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {ImageView i = new ImageView(context);i.setImageResource(mImageids[position]);i.setLayoutParams(new Gallery.LayoutParams(150,100));i.setScaleType(ImageView.ScaleType.FIT_XY);i.setBackgroundResource(mGalleryItemBackground);return i;}}

3,编写一个attrs.xml文件,防止在values文件颊下

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

4,编写主层序类

package com.kang.fei.gallery;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.Toast;public class GalleryActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Gallery g = (Gallery) findViewById(R.id.gallery);        g.setAdapter(new ImageAdapter(this));        g.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(GalleryActivity.this, " "+position,1).show();}        });    }}

更多相关文章

  1. Android重要控件概览(上)
  2. [置顶] android利用jni调用第三方库——第二篇——编写库android
  3. Android(安卓)中插件的编写方法
  4. Cordova与Android之一 Activity跳转
  5. Android欢迎界面引导页
  6. Android(安卓)Component 简介
  7. Android布局LinearLayout子控件无法填充问题
  8. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery
  9. 一个android的按钮

随机推荐

  1. 在Linux中运行Android软件
  2. Android经典项目案例开发实战宝典
  3. 《BAT大咖助力 全面升级Android面试笔记
  4. android 版本更新和下载安装 适配android
  5. 转 android launch flow
  6. phonegap底层原理学习和研究(一)
  7. android LinearLayout添加分隔线
  8. AsyncTask使用和源码解析
  9. android 反编译
  10. golang开发android应用(二) - go语言生成an