xamarin.android 控件,Android 库控件 - Xamarin | Microsoft Docs

Xamarin Android 库控件Xamarin.Android Gallery control

03/15/2018

本文内容

Gallery是一种布局小组件,用于显示水平滚动列表中的项,并将当前所选内容置于视图的中心。Gallery is a layout widget used to display items in a horizontally scrolling list and positions the current selection at the center of the view.

重要

Android 4.1 (API 级别16)已弃用此小组件。This widget was deprecated in Android 4.1 (API level 16).

在本教程中,你将创建一个照片库,并在每次选择库项时显示 toast 消息。In this tutorial, you'll create a gallery of photos and then display a toast message each time a gallery item is selected.

为内容视图设置 Main.axml 布局后,将从具有FindViewById的布局中捕获 Gallery。After the Main.axml layout is set for the content view, the Gallery is captured from the layout with FindViewById.

然后,使用属性将自定义适配器(ImageAdapter)设置为要在 dallery 中显示的所有项的源。property is then used to set a custom adapter ( ImageAdapter) as the source for all items to be displayed in the dallery. ImageAdapter 是在下一步中创建的。The ImageAdapter is created in the next step.

若要在单击库中的某一项时执行操作,请将匿名委托订阅ItemClickTo do something when an item in the gallery is clicked, an anonymous delegate is subscribed to the ItemClick

事件的参数。event. 它显示了一个ToastIt shows a Toast

这会显示 theselected 项的索引位置(从零开始)(在实际情况下,该位置可用于获取其他某个任务的完整大小的图像)。that displays the index position (zero-based) of theselected item (in a real world scenario, the position could be used to get the full sized image for some other task).

首先,有几个成员变量,其中包括引用可绘制资源目录中保存的图像的 Id 的数组(资源/可绘制)。First, there are a few member variables, including an array of IDs that reference the images saved in the drawable resources directory (Resources/drawable).

接下来是类构造函数,其中ContextNext is the class constructor, where the Context

为 ImageAdapter 实例定义并保存到本地字段。for an ImageAdapter instance is defined and saved to a local field.

接下来,这将实现从BaseAdapter继承的一些必需方法。Next, this implements some required methods inherited from BaseAdapter.

构造函数和CountThe constructor and the Count

属性一目了然。property are self-explanatory. 应返回适配器中指定位置的实际对象,但对于此示例,它将被忽略。should return the actual object at the specified position in the adapter, but it's ignored for this example. 应返回项的行 id,但此处不需要。should return the row id of the item, but it's not needed here.

方法执行工作以将映像应用到ImageViewThe method does the work to apply an image to an ImageView

that will be embedded in the Gallery

在此方法中,成员ContextIn this method, the member Context

is used to create a new ImageView.

通过从可绘制资源的本地数组应用映像来准备,并设置Gallery.LayoutParamsis prepared by applying an image from the local array of drawable resources, setting the Gallery.LayoutParams

图像的高度和宽度,将刻度设置为适合ImageViewheight and width for the image, setting the scale to fit the ImageView

维度,最后将背景设置为使用构造函数中获取的 styleable 属性。dimensions, and then finally setting the background to use the styleable attribute acquired in the constructor.

See ImageView.ScaleType for other image scaling options.

演练Walkthrough

启动名为HelloGallery的新项目。Start a new project named HelloGallery.

查找要使用的一些照片,或下载这些示例图像。Find some photos you'd like to use, or download these sample images.

将图像文件添加到项目的资源/可绘制目录。Add the image files to the project's Resources/Drawable directory. 在 "属性" 窗口中,将 "生成操作" 设置为 " AndroidResource"。In the Properties window, set the Build Action for each to AndroidResource.

打开Resources/Layout/main.axml并插入以下内容:Open Resources/Layout/Main.axml and insert the following:

android:id="@+id/gallery"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

打开 MainActivity.cs 并插入以下代码OnCreate()Open MainActivity.cs and insert the following code for the OnCreate()

付款方式method:

protected override void OnCreate (Bundle bundle)

{

base.OnCreate (bundle);

// Set our view from the "main" layout resource

SetContentView (Resource.Layout.Main);

Gallery gallery = (Gallery) FindViewById(Resource.Id.gallery);

gallery.Adapter = new ImageAdapter (this);

gallery.ItemClick += delegate (object sender, Android.Widget.AdapterView.ItemClickEventArgs args) {

Toast.MakeText (this, args.Position.ToString (), ToastLength.Short).Show ();

};

}

创建一个名为的新类 ImageAdapter 子类BaseAdapter:Create a new class called ImageAdapter that subclasses BaseAdapter:

public class ImageAdapter : BaseAdapter

{

Context context;

public ImageAdapter (Context c)

{

context = c;

}

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

public override Java.Lang.Object GetItem (int position)

{

return null;

}

public override long GetItemId (int position)

{

return 0;

}

// create a new ImageView for each item referenced by the Adapter

public override View GetView (int position, View convertView, ViewGroup parent)

{

ImageView i = new ImageView (context);

i.SetImageResource (thumbIds[position]);

i.LayoutParameters = new Gallery.LayoutParams (150, 100);

i.SetScaleType (ImageView.ScaleType.FitXy);

return i;

}

// references to our images

int[] thumbIds = {

Resource.Drawable.sample_1,

Resource.Drawable.sample_2,

Resource.Drawable.sample_3,

Resource.Drawable.sample_4,

Resource.Drawable.sample_5,

Resource.Drawable.sample_6,

Resource.Drawable.sample_7

};

}

运行该应用程序。Run the application. 它应类似于以下屏幕截图:It should look like the screenshot below:

9eefe454153857c2af1928b450c6e5fa.png

referenceReferences

此页面的某些部分是基于 Android 开源项目创建和共享的工作的修改,并根据创造性 Commons 2.5 归属许可证中所述的条款使用。Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值