1、代码
private int[] imageId = new int[]{R.drawable.img01,R.drawable.img02,
R.drawable.img03,R.drawable.img04,R.drawable.img05
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@SuppressWarnings("deprecation")
Gallery gallery = (Gallery)findViewById(R.id.gallery1);
BaseAdapter adapter =new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview;
if(convertView ==null){
imageview=new ImageView(MainActivity.this);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setLayoutParams(new Gallery.LayoutParams(180,135));
// TypedArray typearray=obtainStyledAttributes(android.R.styleable.Gallery);
// imageview.setBackgroundResource(t)
imageview.setPadding(5, 0, 5, 0);
}else{
imageview=(ImageView)convertView;
}
imageview.setImageResource(imageId[position]);
return imageview;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
};
gallery.setAdapter(adapter);
gallery.setSelection(imageId.length/2);
效果