6.1.6 Gallery结合案例详解
Gallery(相册)控件是个很不错的图片查看控件,屏幕中有一个图片列表,Gallery类的继承关系如下:
java.lang.Object
? android.view.View
? android.view.ViewGroup
? android.widget.AdapterView<T extends android.widget.Adapter>
? android.widget.AbsSpinner
? android.widget.Gallery
这个Gallery案例,可以用手滑动Gallery,当用户点击某个图片弹出一个Toast,如6-11图:

6-11 Gallery控件使用效果图
程序代码请参考代码清单6-9:
【代码清单6-9】chapter6_5/src/com/work/GalleryActivity.java
public class GalleryActivity extends Activity {@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 v, int position, long id) { Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); registerForContextMenu(g); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(R.string.gallerytext); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show(); return true; } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); mGalleryItemBackground = a.getResourceId( R.styleable.Gallery1_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 imageView;imageView = new ImageView(mContext);imageView.setImageResource(mImageIds[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));imageView.setBackgroundResource(mGalleryItemBackground);return imageView; } private Context mContext; private Integer[] mImageIds = { R.drawable.beijing, R.drawable.changsha, R.drawable.chengdu, R.drawable.chongqing, R.drawable.haerbing, R.drawable.jinan, R.drawable.jiujiang, R.drawable.kunming, R.drawable.nanjing }; }}<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"android:layout_width="match_parent"android:layout_height="wrap_content"/>

public ImageAdapter(Context c) {mContext = c;TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);a.recycle();}<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="Gallery1"> <attr name="android:galleryItemBackground" /> </declare-styleable></resources>
public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(android.R.styleable.Theme); mGalleryItemBackground = a.getResourceId( android.R.styleable.Theme_galleryItemBackground, 0); a.recycle();}