amxl
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Gallery
android:layout_width="fill_parent"
android:id="@+id/gallery1"
android:layout_height="fill_parent"
android:spacing="16dp" />
</RelativeLayout>
Activity cs
[Activity(Label = "My Activity")]
public class layout1 : Activity
{
private static int[] images = {Resource.Drawable.baos,
Resource.Drawable.caoc,
Resource.Drawable.chenyj,
Resource.Drawable.chenyy,
Resource.Drawable.gouj
};
Gallery gallery;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
SetContentView(Resource.Layout.layout1);
Title="Gallery的效果";
gallery =FindViewById<Gallery>(Resource.Id.gallery1);
ImageAdapter gallimage = new ImageAdapter(this, images);
gallery.Adapter=gallimage;
gallery.SetSelection(images.Length / 2);
gallery.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs args)
{
Toast.MakeText(this, args.Position.ToString(), ToastLength.Short).Show();
};
}
}
重写 BaseAdapter类
public class ImageAdapter:BaseAdapter {
private Context context;
private int[] images;
public ImageAdapter(Context context, int[] images)
{
this.context = context;
this.images = images;
}
public override int Count
{
get {
return images.Length;
}
}
public override long GetItemId(int position)
{
return 0;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView iv = new ImageView(context);
//设置具体要显示的图片
iv.SetImageResource(images[position % images.Length]);
//设置ImageView的高度和宽度
Gallery.LayoutParams paramsatturt = new Gallery.LayoutParams(Gallery.LayoutParams.FillParent,Gallery.LayoutParams.FillParent);
iv.LayoutParameters=paramsatturt;
//设置缩放
iv.SetScaleType(ImageView.ScaleType.FitXy);
iv.SetAdjustViewBounds(true);
return iv;
}
}
源码实例 http://download.csdn.net/detail/hcf_force/7128141