Android开发学习之TypedArray类

     在学习Android的开发中,学习Gallery视图显示图片的过程中,在设置图片适配器的时候,用到了此TypedArray类型,这次根据android SDK,一块把此类型弄清楚!

     android.content.res.TypedArray

     包含函数 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[])检索的数组值。

     在执行完之后,一定要确保调用  recycle()函数 。用于检索从这个结构对应于给定的属性位置到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, int[], int, int)或者

 obtainAttributes(AttributeSet, int[])

定义:

 

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

 

 

public TypedArray obtainAttributes (AttributeSet set, int[] attrs)(说明此函数)

 

说明:返回一个由AttributeSet获得的一系列的基本的属性值,不需要用用一个主题或者/和样式资源执行样式。

参数:

set:现在检索的属性值;

attrs:制定的检索的属性值

public void recycle()

返回先前检索的数组,稍后再用。

 

posted @ 2013-09-17 23:26  pangbangb  阅读(557)  评论(0编辑  收藏  举报