step1 创建atttrs.xml文件用来改变layout 的背景
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
<!-- 定义layout外部resource的attrs.xml文件,用来改变layout的背景图片-->
</resources>
step2修改main_activity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget_absolutelayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Gallery
android:layout_width="fill_parent"
android:layout_height="143px"
android:layout_x="0px"
android:layout_y="51px"
android:id="@+id/Gallery_preView"
/>
<ImageView
android:layout_width="239px"
android:layout_height="218px"
android:layout_x="38px"
android:layout_y="184px"
android:id="@+id/ImageView_photo"
/>
</AbsoluteLayout>
step3创建 MyImageAdapter.java
package com.example.gallerydemo;
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;
import android.widget.ImageView.ScaleType;
/**
* --Gallery 的适配器,它继承于BaseAdapter 类.
*
* @author liudd
*
*/
public class MyImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context context;// 上下文
/* 构建Integer array并取得预加载Drawable的图片id */
public Integer[] myImageIds = { R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6 };
/* 自定义构造函数 */
public MyImageAdapter(Context context) {
this.context = context;
/* 使用在res/values/attrs.xml中的<declare-styleable>定义Gallery属性 */
TypedArray typedArray = context
.obtainStyledAttributes(R.styleable.Gallery);
/* 取得Gallery属性的Index id */
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
/* 让对象的styleable属性能反复使用 */
typedArray.recycle();
}
/*重写方法getCount,返回图片数目*/
public int getCount() {
// TODO Auto-generated method stub
return myImageIds.length;
}
/*重写的方法getItemId,返回图像的数组id*/
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/*重写的方法getView,返回一view对象*/
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/*产生ImageView对象*/
ImageView imageView=new ImageView(context);
/*设置图片给imageView对象*/
imageView.setImageResource(myImageIds[position]);
/*重新设置图片宽高*/
imageView.setScaleType(ScaleType.FIT_XY);
/*重新设置layout的宽高*/
imageView.setLayoutParams(new Gallery.LayoutParams(128,128));
/*设置Gallery背景图*/
imageView.setBackgroundResource(mGalleryItemBackground);
/*返回imageView对象*/
return imageView;
}
}
step4创建MainActivity .java
package com.example.gallerydemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
/**
* Gallery与衍生BaseAdapter容器使用
* @author liudd
*
*/
public class MainActivity extends Activity {
//定义要使用的对象
private Gallery gallery;
private ImageView imageView;
private MyImageAdapter imageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageAdapter=new MyImageAdapter(this);
gallery=(Gallery) findViewById(R.id.Gallery_preView);
imageView=(ImageView) findViewById(R.id.ImageView_photo);
/*给Gallery设置适配器,把当前类传入参数*/
gallery.setAdapter(imageAdapter);
/*设置Gallery的点击事件监听器*/
gallery.setOnItemClickListener(new Gallery.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
/*显示该图片是几号*/
Toast.makeText(MainActivity.this,"这是图片:"+position+"号",Toast.LENGTH_SHORT).show();
/*设置大图片*/
imageView.setBackgroundResource(imageAdapter.myImageIds[position]);
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
注意添加图片的时候图片名称不能是纯数字,必须是字母与数字的组合不然找不到图片。必须是小写字母加数字这种格式【a-z】【0-9】