1. 程式人生 > >Android開發學習之TypedArray類

Android開發學習之TypedArray類

     在學習Android的開發中,學習Gallery檢視顯示圖片的過程中,在設定圖片介面卡的時候,用到了此TypedArray型別,這次根據android SDK,一塊把此型別弄清楚!

     android.content.res.TypedArray

     在執行完之後,一定要確保呼叫  。用於檢索從這個結構對應於給定的屬性位置到obtainStyledAttributes中的值。

    例項:

    自定義attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>
  java中
//---setting the style
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();

  涉及的函式介紹:

定義:

obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

obtainAttributes (AttributeSet set, int[] attrs)(說明此函式)

說明:返回一個由AttributeSet獲得的一系列的基本的屬性值,不需要用用一個主題或者/和樣式資源執行樣式。

引數:

set:現在檢索的屬性值;

attrs:制定的檢索的屬性值

public void recycle()

返回先前檢索的陣列,稍後再用。