Most visited

Recently visited

Added in API level 1

TypedArray

public class TypedArray
extends Object

java.lang.Object
   ↳ android.content.res.TypedArray


obtainStyledAttributes(AttributeSet, int[], int, int)obtainAttributes(AttributeSet, int[])检索的值数组的容器。 完成后请务必致电recycle() 用于从此结构检索值的索引对应于给予obtainStyledAttributes的属性的位置。

Summary

Public methods

boolean getBoolean(int index, boolean defValue)

检索 索引处属性的布尔值。

int getChangingConfigurations()

返回此类型数组中的值可能更改的配置参数的掩码。

int getColor(int index, int defValue)

检索 索引处属性的颜色值。

ColorStateList getColorStateList(int index)

检索 索引处属性的ColorStateList。

float getDimension(int index, float defValue)

索引处检索维度单位属性。

int getDimensionPixelOffset(int index, int defValue)

索引处检索维度单位属性,以用作原始像素中的偏移量。

int getDimensionPixelSize(int index, int defValue)

索引处检索维度单位属性以用作原始像素的大小。

Drawable getDrawable(int index)

索引处检索属性的Drawable。

float getFloat(int index, float defValue)

索引处检索属性的浮点值。

float getFraction(int index, int base, int pbase, float defValue)

索引处检索小数单位属性。

int getIndex(int at)

返回包含数据的数组中的索引。

int getIndexCount()

返回数组中实际具有数据的索引数。

int getInt(int index, int defValue)

检索 索引处属性的整数值。

int getInteger(int index, int defValue)

检索 索引处属性的整数值。

int getLayoutDimension(int index, int defValue)

用于检索 ViewGroup的layout_width和layout_height属性的特殊版本 getDimensionPixelSize(int, int)

int getLayoutDimension(int index, String name)

getDimensionPixelSize(int, int)特殊版本,用于检索 ViewGroup的layout_width和layout_height属性。

String getNonResourceString(int index)

检索 索引处属性的字符串值,但仅限于该字符串来自XML文件中的立即值。

String getPositionDescription()

返回有关适用于打印错误消息的解析器状态的消息。

int getResourceId(int index, int defValue)

检索 索引处属性的资源标识符。

Resources getResources()

返回此数组从其加载的资源对象。

String getString(int index)

检索 索引处属性的字符串值。

CharSequence getText(int index)

检索 索引处属性的样式化字符串值。

CharSequence[] getTextArray(int index)

检索 索引处属性的CharSequence []。

int getType(int index)

返回指定索引处的属性类型。

boolean getValue(int index, TypedValue outValue)

检索 索引处属性的原始TypedValue。

boolean hasValue(int index)

确定 索引处是否有属性。

boolean hasValueOrEmpty(int index)

确定是否有 索引的属性,返回 true如果属性被显式设置为 @emptyfalse只有当属性是不确定的。

int length()

返回此数组中的值的数量。

TypedValue peekValue(int index)

检索 index处属性的原始TypedValue,并返回保存其数据的临时对象。

void recycle()

回收TypedArray,以供稍后调用者重新使用。

String toString()

返回对象的字符串表示形式。

Inherited methods

From class java.lang.Object

Public methods

getBoolean

Added in API level 1
boolean getBoolean (int index, 
                boolean defValue)

检索 索引处属性的布尔值。

如果该属性是一个整数值,则此方法将返回它是否等于零。 如果属性不是布尔值或整数值,则此方法将尝试使用decode(String)将其强制为整数,并返回它是否等于零。

Parameters
index int: Index of attribute to retrieve.
defValue boolean: Value to return if the attribute is not defined or cannot be coerced to an integer.
Returns
boolean Boolean value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.
Throws
RuntimeException if the TypedArray has already been recycled.

getChangingConfigurations

Added in API level 21
int getChangingConfigurations ()

返回此类型数组中的值可能更改的配置参数的掩码。

Returns
int Returns a mask of the changing configuration parameters, as defined by ActivityInfo.
Throws
RuntimeException if the TypedArray has already been recycled.

也可以看看:

getColor

Added in API level 1
int getColor (int index, 
                int defValue)

检索索引处属性的颜色值。 如果该属性引用持有复杂的颜色资源ColorStateList ,则返回该集合中的默认颜色。

如果该属性已定义但该属性不是整数颜色或颜色状态列表,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or not a resource.
Returns
int Attribute color value, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not an integer color or color state list.

getColorStateList

Added in API level 1
ColorStateList getColorStateList (int index)

检索索引处属性的ColorStateList。 该值可以是一个单一的固体彩色或颜色或复杂的参考ColorStateList描述。

如果属性未定义或者不是整数颜色或颜色状态列表,则此方法将返回 null

Parameters
index int: Index of attribute to retrieve.
Returns
ColorStateList ColorStateList for the attribute, or null if not defined.
Throws
RuntimeException if the attribute if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not an integer color or color state list.

getDimension

Added in API level 1
float getDimension (int index, 
                float defValue)

索引处检索维度单位属性。 单位转换基于与此TypedArray对象来自的资源相关联的当前DisplayMetrics

如果该属性已定义但不是维度,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
defValue float: Value to return if the attribute is not defined or not a resource.
Returns
float Attribute dimension value multiplied by the appropriate metric, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not an integer.

也可以看看:

getDimensionPixelOffset

Added in API level 1
int getDimensionPixelOffset (int index, 
                int defValue)

索引处检索维度单位属性,以用作原始像素中的偏移量。 这与getDimension(int, float)相同,只是返回的值将转换为整数像素。 偏移转换涉及将基本值简单地截断为整数。

如果该属性已定义但不是维度,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or not a resource.
Returns
int Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not an integer.

也可以看看:

getDimensionPixelSize

Added in API level 1
int getDimensionPixelSize (int index, 
                int defValue)

索引处检索维度单位属性以用作原始像素的大小。 这与getDimension(int, float)相同,只是返回的值被转换为整数像素以用作大小。 大小转换包括舍入基值,并确保非零基值至少有一个像素大小。

如果该属性已定义但不是维度,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or not a resource.
Returns
int Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not a dimension.

也可以看看:

getDrawable

Added in API level 1
Drawable getDrawable (int index)

索引处检索属性的Drawable。

如果该属性已定义但该属性不是颜色或可绘制资源,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
Returns
Drawable Drawable for the attribute, or null if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not a color or drawable resource.

getFloat

Added in API level 1
float getFloat (int index, 
                float defValue)

索引处检索属性的浮点值。

如果该属性不是浮点数或整数,则此方法将尝试使用 parseFloat(String)将其强制转换为浮点数。

Parameters
index int: Index of attribute to retrieve.
defValue float
Returns
float Attribute float value, or defValue if the attribute was not defined or could not be coerced to a float.
Throws
RuntimeException if the TypedArray has already been recycled.

getFraction

Added in API level 1
float getFraction (int index, 
                int base, 
                int pbase, 
                float defValue)

索引处检索小数单位属性。

Parameters
index int: Index of attribute to retrieve.
base int: The base value of this fraction. In other words, a standard fraction is multiplied by this value.
pbase int: The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
defValue float: Value to return if the attribute is not defined or not a resource.
Returns
float Attribute fractional value multiplied by the appropriate base value, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not a fraction.

getIndex

Added in API level 1
int getIndex (int at)

返回包含数据的数组中的索引。

Parameters
at int: The index you would like to returned, ranging from 0 to getIndexCount().
Returns
int The index at the given offset, which can be used with getValue(int, TypedValue) and related APIs.
Throws
RuntimeException if the TypedArray has already been recycled.

getIndexCount

Added in API level 1
int getIndexCount ()

返回数组中实际具有数据的索引数。

Returns
int
Throws
RuntimeException if the TypedArray has already been recycled.

getInt

Added in API level 1
int getInt (int index, 
                int defValue)

检索 索引处属性的整数值。

如果该属性不是整数,则此方法将尝试使用 decode(String)将其强制转换为整数。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or cannot be coerced to an integer.
Returns
int Integer value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.
Throws
RuntimeException if the TypedArray has already been recycled.

getInteger

Added in API level 1
int getInteger (int index, 
                int defValue)

检索 索引处属性的整数值。

getInt(int, int)不同,如果该属性被定义但不是整数,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or not a resource.
Returns
int Attribute integer value, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not an integer.

getLayoutDimension

Added in API level 3
int getLayoutDimension (int index, 
                int defValue)

getDimensionPixelSize(int, int)特殊版本,用于检索ViewGroup的layout_width和layout_height属性。 这只是出于性能原因; 应用程序应该使用getDimensionPixelSize(int, int)

Parameters
index int: Index of the attribute to retrieve.
defValue int: The default value to return if this attribute is not default or contains the wrong type of data.
Returns
int Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws
RuntimeException if the TypedArray has already been recycled.

getLayoutDimension

Added in API level 1
int getLayoutDimension (int index, 
                String name)

getDimensionPixelSize(int, int)特殊版本,用于检索ViewGroup的layout_width和layout_height属性。 这只是出于性能原因; 应用程序应使用getDimensionPixelSize(int, int)

如果定义属性但不是维或整数(枚举),则此方法将引发异常。

Parameters
index int: Index of the attribute to retrieve.
name String: Textual name of attribute for error reporting.
Returns
int Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws
RuntimeException if the TypedArray has already been recycled.
UnsupportedOperationException if the attribute is defined but is not a dimension or integer (enum).

getNonResourceString

Added in API level 1
String getNonResourceString (int index)

检索索引处属性的字符串值,但仅限于该字符串来自XML文件中的立即值。 也就是说,这不允许引用字符串资源,字符串属性或来自其他类型的转换。 因此,此方法只会返回来自XML文件中属性的TypedArray对象的字符串。

Parameters
index int: Index of attribute to retrieve.
Returns
String String holding string data. Any styling information is removed. Returns null if the attribute is not defined or is not an immediate string value.
Throws
RuntimeException if the TypedArray has already been recycled.

getPositionDescription

Added in API level 1
String getPositionDescription ()

返回有关适用于打印错误消息的解析器状态的消息。

Returns
String Human-readable description of current parser state.
Throws
RuntimeException if the TypedArray has already been recycled.

getResourceId

Added in API level 1
int getResourceId (int index, 
                int defValue)

检索索引处属性的资源标识符。 请注意,在检索整个TypedArray对象时已解析属性资源。 因此,该函数将返回找到的最终资源值的资源标识符, 不一定是该属性指定的原始资源。

Parameters
index int: Index of attribute to retrieve.
defValue int: Value to return if the attribute is not defined or not a resource.
Returns
int Attribute resource identifier, or defValue if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.

getResources

Added in API level 1
Resources getResources ()

返回此数组从其加载的资源对象。

Returns
Resources
Throws
RuntimeException if the TypedArray has already been recycled.

getString

Added in API level 1
String getString (int index)

检索 索引处属性的字符串值。

如果该属性不是字符串,则此方法将尝试将其强制转换为字符串。

Parameters
index int: Index of attribute to retrieve.
Returns
String String holding string data. Any styling information is removed. Returns null if the attribute is not defined or could not be coerced to a string.
Throws
RuntimeException if the TypedArray has already been recycled.

getText

Added in API level 1
CharSequence getText (int index)

检索 索引处属性的样式化字符串值。

如果该属性不是字符串,则此方法将尝试将其强制转换为字符串。

Parameters
index int: Index of attribute to retrieve.
Returns
CharSequence CharSequence holding string data. May be styled. Returns null if the attribute is not defined or could not be coerced to a string.
Throws
RuntimeException if the TypedArray has already been recycled.

getTextArray

Added in API level 1
CharSequence[] getTextArray (int index)

检索索引处属性的CharSequence []。 这会获取选定属性的资源ID,并使用拥有的Resources对象的Resources.getTextArray来检索其String []。

如果定义属性但不是文本数组资源,则此方法将引发异常。

Parameters
index int: Index of attribute to retrieve.
Returns
CharSequence[] CharSequence[] for the attribute, or null if not defined.
Throws
RuntimeException if the TypedArray has already been recycled.

getType

Added in API level 21
int getType (int index)

返回指定索引处的属性类型。

Parameters
index int: Index of attribute whose type to retrieve.
Returns
int Attribute type.
Throws
RuntimeException if the TypedArray has already been recycled.

getValue

Added in API level 1
boolean getValue (int index, 
                TypedValue outValue)

检索 索引处属性的原始TypedValue。

Parameters
index int: Index of attribute to retrieve.
outValue TypedValue: TypedValue object in which to place the attribute's data.
Returns
boolean true if the value was retrieved, false otherwise.
Throws
RuntimeException if the TypedArray has already been recycled.

hasValue

Added in API level 1
boolean hasValue (int index)

确定 索引处是否有属性。

注意:如果该属性设置为 @empty@undefined ,则此方法返回 false

Parameters
index int: Index of attribute to retrieve.
Returns
boolean True if the attribute has a value, false otherwise.
Throws
RuntimeException if the TypedArray has already been recycled.

hasValueOrEmpty

Added in API level 22
boolean hasValueOrEmpty (int index)

确定在 索引处是否存在属性,如果该属性仅在属性未定义时明确设置为 @emptyfalse ,则返回 true

Parameters
index int: Index of attribute to retrieve.
Returns
boolean True if the attribute has a value or is empty, false otherwise.
Throws
RuntimeException if the TypedArray has already been recycled.

length

Added in API level 1
int length ()

返回此数组中的值的数量。

Returns
int
Throws
RuntimeException if the TypedArray has already been recycled.

peekValue

Added in API level 1
TypedValue peekValue (int index)

检索index处属性的原始TypedValue,并返回保存其数据的临时对象。 此对象仅在下一次调用TypedArray之前有效。

Parameters
index int: Index of attribute to retrieve.
Returns
TypedValue Returns a TypedValue object if the attribute is defined, containing its data; otherwise returns null. (You will not receive a TypedValue whose type is TYPE_NULL.)
Throws
RuntimeException if the TypedArray has already been recycled.

recycle

Added in API level 1
void recycle ()

回收TypedArray,以供稍后调用者重新使用。 调用这个函数后,你不能再次触摸类型数组。

Throws
RuntimeException if the TypedArray has already been recycled.

toString

Added in API level 1
String toString ()

返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本地表示”这个对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

Hooray!