Xamarin.Android 資源庫控件

Gallery 是一個版面配置小工具,用來在水平卷動清單中顯示專案,並將目前選取範圍放在檢視中央。

重要

此小工具在Android 4.1 中已被取代(API 層級 16)。

在本教學課程中,您將建立相片資源庫,然後在每次選取資源庫項目時顯示快顯通知訊息。

設定內容檢視的設定 Main.axml 之後, Gallery 會使用 從版面配置 FindViewById擷取 。 Adapter 屬性接著會用來將自定義配接器 ( ImageAdapter) 設定為要顯示在 dallery 中的所有專案的來源。 會在 ImageAdapter 下一步驟中建立 。

若要在單擊資源庫中的專案時執行某些動作,匿名委派會訂閱 ItemClick 事件。 它會顯示 Toast ,顯示所選取專案的索引位置(以零起始的)(在真實世界案例中,這個位置可用來取得其他工作的完整大小影像)。

首先,有幾個成員變數,包括參考儲存在可繪製資源目錄中之影像的標識符陣列(Resources/drawable)。

接下來是 類別建構函式,其中 ContextImageAdapter實例的定義和儲存到本機欄位。 接下來,這會實作繼承自 BaseAdapter的一些必要方法。 建構函式和 Count 屬性是自我說明的。 通常 GetItem(int) 應該傳回位於配接器中指定位置的實際物件,但本範例會忽略它。 同樣 GetItemId(int) 應該傳回專案的數據列標識符,但這裡不需要。

方法會執行將影像套用至 的工作 ImageView 將內嵌在中 Gallery 在此方法中,成員 Context 是用來建立新的 ImageViewImageView 從可繪製資源的本機數位套用影像來準備,設定 Gallery.LayoutParams 影像的高度和寬度,將縮放比例設定為符合 ImageView 維度,最後將背景設定為使用建構函式中取得的可設定樣式屬性。

如需其他影像縮放選項,請參閱 ImageView.ScaleType

逐步解說

啟動名為 HelloGallery 的新專案。

Screenshot of new Android project in the New Solution dialog

尋找您想要使用的一些相片,或 下載這些範例影像。 將圖像檔新增至專案的 Resources/Drawable 目錄。 在 [ 屬性] 視窗中,將每個的 [建置動作] 設定為 [AndroidResource]。

開啟 Resources/Layout/Main.axml 並插入下列專案:

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

開啟 MainActivity.cs 並插入下列程序代碼 OnCreate() 方法:

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<Gallery>(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的新類別:

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
     };
}

執行應用程式。 它看起來應該像下面的螢幕快照:

Screenshot of HelloGallery displaying sample images

參考資料

此頁面的部分是根據 Android 開放原始碼專案所建立和共用的工作進行修改,並根據 Creative Commons 2.5 屬性授權中所述的詞彙使用。