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.youkuaiyun.com/detail/hcf_force/7128141

本文详细介绍了如何通过重写BaseAdapter类来实现自定义GALLERY效果,包括创建图片适配器、设置布局参数、调整缩放以及响应点击事件等关键步骤。
4133

被折叠的 条评论
为什么被折叠?



