Android TypedArray类 详解
TypedArray类介绍
obtainStyledAttributes(AttributeSet, int[], int, int)或obtainAttributes(AttributeSet, int[])检索的值数组。一定要在使用完它们后调用recycle()。用于从该结构中检索值的索引对应于给定给obtainStyledAttributes的属性的位置
TypedArray类的方法
方法 | 说明 |
---|---|
public boolean getBoolean (int index, boolean defValue) 检索索引处属性的布尔值 | index: 要检索的属性索引 defValue属性未定义时使用的默认值 |
public int getColor (int index, int defValue) 检索索引处属性的颜色值。如果属性引用一个包含复杂ColorStateList的颜色资源,则返回该集合的默认颜色 | index: 要检索的属性索引 defValue: 属性未定义或不是资源使用的默认值 |
public ColorStateList getColorStateList (int index) 检索索引处属性的ColorStateList。该值可以是单一纯色,也可以是对颜色或复杂的ColorStateList描述的引用 | index: 要检索的属性索引 注意: ColorStateList表示属性,如果未定义,则为null |
public float getDimension (int index, float defValue) 在索引处检索维度单位属性 | index:要检索的属性索引 .defValue:属性未定义或不是资源使用的默认值 |
public int getDimensionPixelOffset (int index, int defValue) 在索引处检索维度单位属性,用作原始像素的偏移量;属性维值乘以适当的度量,并被截断为整数像素 | index:要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 |
public int getDimensionPixelSize (int index, int defValue) 在索引处检索维度单位属性,作为原始像素的大小使用;属性维值乘以适当的度量,并被截断为整数像素 | index:要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 注意: 并确保非零的基础值在大小上至少是一个像素 |
public int getLayoutDimension (int index, String name) 属性维值乘以适当的度量,并截断为整数像素 | index:要检索的属性索引 name:用于错误报告的属性的文本名称 注意:getDimensionPixelSize(int, int)的特殊版本;用于检索ViewGroup的layout_width和layout_height属性;出于性能考虑应用应该使用getDimensionPixelSize(int, int) |
public int getLayoutDimension (int index, int defValue) 属性维值乘以适当的度量,并截断为整数像素 | index:要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 注意:getDimensionPixelSize(int, int)的特殊版本;用于检索ViewGroup的layout_width和layout_height属性;出于性能考虑应用应该使用getDimensionPixelSize(int, int) |
public Drawable getDrawable (int index) 检索索引处属性的Drawable。这将获取所选属性的资源ID,并使用资源。 | index: 要检索的属性索引 注意:如果未定义,则为null |
public float getFloat (int index, float defValue) 检索索引处属性的浮点值 | index: 要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 |
public float getFraction (int index, int base, int pbase, float defValue) 在索引处检索一个分数单位属性 | index:要检索的属性索引 base:分数的基值;一个标准分数乘以这个值 pbase 分数的父基值;一个父分数(nn%p)乘以这个值 属性未定义或不是资源使用的默认值 注意:属性的分数值乘以适当的基值,如果没有定义,则为差值 |
public int getIndexCount () 返回数组中实际包含数据的索引数 | |
public int getIndex (int at) 返回包含数据的数组中的索引 | at:希望返回的索引,范围从0到getIndexCount() |
public int getInt (int index, int defValue) 检索索引处属性的整数值 | index:要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 |
public int getInteger (int index, int defValue) 检索索引处属性的整数值 | index:要检索的属性索引 defValue:属性未定义或不是资源使用的默认值 |
public String getNonResourceString (int index) 此方法将只返回来自XML文件中的属性的TypedArray对象的字符串;但仅当该字符串来自XML文件中的即时值(不允许引用字符串资源、字符串属性或从其他类型进行转换) | index:要检索的属性索引 注意:如果属性未定义或不是立即的字符串值,则返回null |
public int getResourceId (int index, int defValue) 返回索引处属性的资源标识符 | index: 要检索的属性索引 defValue: 属性未定义或不是资源使用的默认值 注意:在检索整个TypedArray对象时解析的属性资源。因此,此函数将返回找到的最终资源值的资源标识符,而不一定是由属性指定的原始资源 |
public Resources getResources () 返回加载此数组的资源对象 | |
public String getString (int index) 返回索引处属性的字符串值 | index:要检索的属性索引 如果未定义属性,则返回null |
public CharSequence getText (int index) 返回索引处属性的样式字符串值 | index:要检索的属性索引 注意:如果未定义属性,则返回null |
public CharSequence[] getTextArray (int index) 检索索引处属性的CharSequence[]。这将获取所选属性的资源ID,并使用资源 | index: 要检索的属性索引 注意:如果未定义属性,则返回null |
public boolean getValue (int index, TypedValue outValue) 检索索引处属性的原始类型值 | index: 要检索的属性索引 outValue:放置的属性数据TypedValue对象 |
public boolean hasValue (int index) 索引处是否有属性 | index: 要检索的属性索引 |
public int length () 返回数组中值的个数 | |
public TypedValue peekValue (int index) 检索索引处属性的原始TypedValue,并返回一个保存其数据的临时对象 | index: 要检索的属性索引 注意:此对象仅在下一次调用TypedArray之前有效 如果属性已定义,则返回TypedValue对象,其中包含其数据;否则返回null |
public void recycle () 返回先前检索到的数组,以便以后重用 | 注意:TypedArray对象是共享资源,必须使用后进行回收 |
public String toString () 返回一个字符串,该字符串包含对该对象的简明的、人类可读的描述 | 注意:鼓励子类重写此方法,并提供考虑到对象类型和数据的实现 |