Xamarin 简明教程

库房是一种用来显示水平可滚动列表中项的视图类型。然后,选定项目就会显示在中心。在此示例中,您将创建一个包含水平可滚动图像的库房。单击图像后,将显示选定图像的数字。

A Gallery is a type of view that is used to show items in a horizontal scrollable list. The selected item is then shown at the center. In this example, you are going to create a gallery containing images which are scrollable horizontally. An image when clicked will display a number for the selected image.

首先,创建一个项目并指定一个名称,例如“库房应用程序教程”。在您开始编码前,将图片粘贴到 resource /drawable folder 中。导航到 resources folder 中的 main.axml ,以及线性布局标记之间的库房。

First of all, create a new project and give it a name, e.g., Gallery App Tutorial. Before you start to code, paste 7 images into the resource /drawable folder. Navigate to main.axml under resources folder and a gallery in between the linear layout tags.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#d3d3d3">
   <Gallery
      android:id="@+id/gallery"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="10dp" />
</LinearLayout>

创建一个名为 ImageAdapter 的新类。此类将用于将图像绑定到我们在上面创建的库房中。

Create a new class called ImageAdapter. This class is going to be used to bind the images to the gallery we created above.

第一步是添加一个类,其中包含我们用于存储字段的 cont 上下文。

The first step is to add a class that contains a context cont which we use to store fields.

public class ImageAdapter : BaseAdapter {
   Context cont;
   public ImageAdapter(Context ct) {
      cont = ct;
   }
}

接下来,我们计算图像数组,并返回其大小。

Next, we count the array list which contains our image and returns its size.

public override int Count {
   get {
      return imageArraylist.Length;
   }
}

在下一步中,我们获取项目的坐标。以下代码演示了如何执行此操作。

In the next step, we get the position of the item. The following code shows how to do it.

public override Java.Lang.Object GetItem(int position) {
   return null;
}
public override long GetItemId(int position) {
   return 0;
}

在下一步中,我们创建 imageview ,用于适配器引用的项目。

In the next step, we create an imageview for the items referenced by the adapter.

public override View GetView(int position,View convertView, ViewGroup parent) {
   ImageView img = new ImageView(cont);
   img.SetImageResource(imageArraylist[position]);
   img.SetScaleType(ImageView.ScaleType.FitXy);
   img.LayoutParameters = new Gallery.LayoutParams(200, 100);
   return img;
}

在最后一步中,我们创建 resources.drawable 文件夹中添加的图像引用。为执行此操作,我们创建一个存储图库的数组。以下代码说明了如何执行此操作。

In the final step, we create a reference to the images we added in the resources.drawable folder. To do this, we create an array to hold the collection of images. The following code explains how to do it.

int[] imageArraylist = {
   Resource.Drawable.img1,
   Resource.Drawable.img2,
   Resource.Drawable.img3,
   Resource.Drawable.img4,
   Resource.Drawable.img5,
   Resource.Drawable.img6,
  };
}

接下来,我们转到 mainActivity.cs 并在 OnCreate() 方法下插入以下代码。

Next, we go to mainActivity.cs and insert the following code under the OnCreate() method.

Gallery myGallery = (Gallery)FindViewById<Gallery>(Resource.Id.gallery);
myGallery.Adapter = new ImageAdapter(this);
myGallery.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs args) {
   Toast.MakeText(this,
      args.Position.ToString(), ToastLength.Short).Show();
}

最后,构建并运行应用程序来查看输出。

Finally, build and run your application to view the output.

gallery