日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2024/9/18 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

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.

總結

以上是生活随笔為你收集整理的xamarin.android 控件,Android 库控件 - Xamarin | Microsoft Docs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。