android-TypedArray

来源:互联网 发布:小众手表 知乎 编辑:程序博客网 时间:2024/05/24 06:55

TypedArray

public class TypedArray 
extends Object 

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

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) orobtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

容器与obtainStyledAttributes检索的值是一个数组(AttributeSet,int[],int,int)orobtainAttributes(AttributeSet,int[])。一定要调用recycle()完成后()。从这个结构指数用于检索值对应的位置给obtainStyledAttributes属性。

Summary


Public methods

booleangetBoolean(int index, boolean defValue)

Retrieve the boolean value for the attribute at index.在索引检索属性的布尔值。

intgetChangingConfigurations()

Return a mask of the configuration parameters for which the values in this typed array may change.

intgetColor(int index, int defValue)

Retrieve the color value for the attribute at index.在索引检索属性的颜色值。

ColorStateListgetColorStateList(int index)

Retrieve the ColorStateList for the attribute at index.检索ColorStateList属性的索引。

floatgetDimension(int index, float defValue)

Retrieve a dimensional unit attribute at index.在索引检索空间单元属性。

intgetDimensionPixelOffset(int index, int defValue)

Retrieve a dimensional unit attribute at index for use as an offset in raw pixels.检索一个空间单元属性在指数作为抵消原始像素。

intgetDimensionPixelSize(int index, int defValue)

Retrieve a dimensional unit attribute at index for use as a size in raw pixels.检索一个空间单元属性指数作为原始像素的大小。

DrawablegetDrawable(int index)

Retrieve the Drawable for the attribute at index.检索的索引属性。

floatgetFloat(int index, float defValue)

Retrieve the float value for the attribute at index.在索引检索的浮动值属性。

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

Retrieves a fractional unit attribute at index.在索引检索部分单元属性。

intgetIndex(int at)

Returns an index in the array that has data.返回一个数组中的索引数据。

intgetIndexCount()

Return the number of indices in the array that actually have data.返回数组中的索引的数量数据。

intgetInt(int index, int defValue)

Retrieve the integer value for the attribute at index.在索引中获取属性的整数值。

intgetInteger(int index, int defValue)

Retrieve the integer value for the attribute at index.在索引中获取属性的整数值。

intgetLayoutDimension(int index, int defValue)

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.特别版的getDimensionPixelSize(int,int)为检索ViewGroup layout_width layout_height属性。

intgetLayoutDimension(int index, String name)

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.

StringgetNonResourceString(int index)

Retrieves the string value for the attribute at index, but only if that string comes from an immediate value in an XML file.

检索的字符串值属性在指数,但前提是该字符串来自立即值在一个XML文件中。

StringgetPositionDescription()

Returns a message about the parser state suitable for printing error messages.返回一个消息解析器的状态适合打印错误消息。

intgetResourceId(int index, int defValue)

Retrieves the resource identifier for the attribute at index.在索引检索的资源标识符属性。

ResourcesgetResources()

Returns the Resources object this array was loaded from.返回这个数组被加载的资源对象。

StringgetString(int index)

Retrieves the string value for the attribute at index.在索引检索属性的字符串值。

CharSequencegetText(int index)

Retrieves the styled string value for the attribute at index.检索样式属性的字符串值指数。

CharSequence[]getTextArray(int index)

Retrieve the CharSequence[] for the attribute at index.检索CharSequence进行[]属性的索引。

intgetType(int index)

Returns the type of attribute at the specified index.

booleangetValue(int index, TypedValue outValue)

Retrieve the raw TypedValue for the attribute at index.检索原始TypedValue属性的索引。

booleanhasValue(int index)

Determines whether there is an attribute at index.决定是否有一个属性在索引。

booleanhasValueOrEmpty(int index)

Determines whether there is an attribute at index, returning true if the attribute was explicitly set to @empty and false only if the attribute was undefined.

intlength()

Returns the number of values in this array.返回这个数组中的值的数量。

TypedValuepeekValue(int index)

Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data.检索原始TypedValue属性的索引和返回一个临时对象持有它的数据。

voidrecycle()

Recycles the TypedArray, to be re-used by a later caller.回收TypedArray,被后面的调用者重新使用。

StringtoString()

Returns a string representation of the object.返回一个对象的字符串表示。

Inherited methods

From class java.lang.Object

Public methods


getBoolean

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

Retrieve the boolean value for the attribute at index.

If the attribute is an integer value, this method will return whether it is equal to zero. If the attribute is not a boolean or integer value, this method will attempt to coerce it to an integer using decode(String) and return whether it is equal to zero.

Parametersindexint: Index of attribute to retrieve.defValueboolean: Value to return if the attribute is not defined or cannot be coerced to an integer.ReturnsbooleanBoolean value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getChangingConfigurations

Added in API level 21
int getChangingConfigurations ()

Return a mask of the configuration parameters for which the values in this typed array may change.

ReturnsintReturns a mask of the changing configuration parameters, as defined by ActivityInfo.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

See also:

  • ActivityInfo

getColor

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

Retrieve the color value for the attribute at index. If the attribute references a color resource holding a complex ColorStateList, then the default color from the set is returned.

This method will throw an exception if the attribute is defined but is not an integer color or color state list.

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or not a resource.ReturnsintAttribute color value, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not an integer color or color state list.

getColorStateList

Added in API level 1
ColorStateList getColorStateList (int index)

Retrieve the ColorStateList for the attribute at index. The value may be either a single solid color or a reference to a color or complex ColorStateListdescription.

This method will return null if the attribute is not defined or is not an integer color or color state list.

Parametersindexint: Index of attribute to retrieve.ReturnsColorStateListColorStateList for the attribute, or null if not defined.ThrowsRuntimeExceptionif the attribute if the TypedArray has already been recycled.UnsupportedOperationExceptionif 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)

Retrieve a dimensional unit attribute at index. Unit conversions are based on the current DisplayMetrics associated with the resources this TypedArrayobject came from.

This method will throw an exception if the attribute is defined but is not a dimension.

Parametersindexint: Index of attribute to retrieve.defValuefloat: Value to return if the attribute is not defined or not a resource.ReturnsfloatAttribute dimension value multiplied by the appropriate metric, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not an integer.

See also:

  • getDimensionPixelOffset(int, int)
  • getDimensionPixelSize(int, int)

getDimensionPixelOffset

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

Retrieve a dimensional unit attribute at index for use as an offset in raw pixels. This is the same as getDimension(int, float), except the returned value is converted to integer pixels for you. An offset conversion involves simply truncating the base value to an integer.

This method will throw an exception if the attribute is defined but is not a dimension.

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or not a resource.ReturnsintAttribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not an integer.

See also:

  • getDimension(int, float)
  • getDimensionPixelSize(int, int)

getDimensionPixelSize

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

Retrieve a dimensional unit attribute at index for use as a size in raw pixels. This is the same as getDimension(int, float), except the returned value is converted to integer pixels for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size.

This method will throw an exception if the attribute is defined but is not a dimension.

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or not a resource.ReturnsintAttribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not a dimension.

See also:

  • getDimension(int, float)
  • getDimensionPixelOffset(int, int)

getDrawable

Added in API level 1
Drawable getDrawable (int index)

Retrieve the Drawable for the attribute at index.

This method will throw an exception if the attribute is defined but is not a color or drawable resource.

Parametersindexint: Index of attribute to retrieve.ReturnsDrawableDrawable for the attribute, or null if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not a color or drawable resource.

getFloat

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

Retrieve the float value for the attribute at index.

If the attribute is not a float or an integer, this method will attempt to coerce it to a float using parseFloat(String).

Parametersindexint: Index of attribute to retrieve.defValuefloatReturnsfloatAttribute float value, or defValue if the attribute was not defined or could not be coerced to a float.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getFraction

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

Retrieves a fractional unit attribute at index.

Parametersindexint: Index of attribute to retrieve.baseint: The base value of this fraction. In other words, a standard fraction is multiplied by this value.pbaseint: The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.defValuefloat: Value to return if the attribute is not defined or not a resource.ReturnsfloatAttribute fractional value multiplied by the appropriate base value, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not a fraction.

getIndex

Added in API level 1
int getIndex (int at)

Returns an index in the array that has data.

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

getIndexCount

Added in API level 1
int getIndexCount ()

Return the number of indices in the array that actually have data.

Returnsint ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getInt

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

Retrieve the integer value for the attribute at index.

If the attribute is not an integer, this method will attempt to coerce it to an integer using decode(String).

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or cannot be coerced to an integer.ReturnsintInteger value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getInteger

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

Retrieve the integer value for the attribute at index.

Unlike getInt(int, int), this method will throw an exception if the attribute is defined but is not an integer.

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or not a resource.ReturnsintAttribute integer value, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not an integer.

getLayoutDimension

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

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize(int, int).

Parametersindexint: Index of the attribute to retrieve.defValueint: The default value to return if this attribute is not default or contains the wrong type of data.ReturnsintAttribute dimension value multiplied by the appropriate metric and truncated to integer pixels.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getLayoutDimension

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

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize(int, int).

This method will throw an exception if the attribute is defined but is not a dimension or integer (enum).

Parametersindexint: Index of the attribute to retrieve.nameString: Textual name of attribute for error reporting.ReturnsintAttribute dimension value multiplied by the appropriate metric and truncated to integer pixels.ThrowsRuntimeExceptionif the TypedArray has already been recycled.UnsupportedOperationExceptionif the attribute is defined but is not a dimension or integer (enum).

getNonResourceString

Added in API level 1
String getNonResourceString (int index)

Retrieves the string value for the attribute at index, but only if that string comes from an immediate value in an XML file. That is, this does not allow references to string resources, string attributes, or conversions from other types. As such, this method will only return strings for TypedArray objects that come from attributes in an XML file.

Parametersindexint: Index of attribute to retrieve.ReturnsStringString holding string data. Any styling information is removed. Returns null if the attribute is not defined or is not an immediate string value.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getPositionDescription

Added in API level 1
String getPositionDescription ()

Returns a message about the parser state suitable for printing error messages.

ReturnsStringHuman-readable description of current parser state.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getResourceId

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

Retrieves the resource identifier for the attribute at index. Note that attribute resource as resolved when the overall TypedArray object is retrieved. As a result, this function will return the resource identifier of the final resource value that was found, not necessarily the original resource that was specified by the attribute.

Parametersindexint: Index of attribute to retrieve.defValueint: Value to return if the attribute is not defined or not a resource.ReturnsintAttribute resource identifier, or defValue if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getResources

Added in API level 1
Resources getResources ()

Returns the Resources object this array was loaded from.

ReturnsResources ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getString

Added in API level 1
String getString (int index)

Retrieves the string value for the attribute at index.

If the attribute is not a string, this method will attempt to coerce it to a string.

Parametersindexint: Index of attribute to retrieve.ReturnsStringString holding string data. Any styling information is removed. Returns null if the attribute is not defined or could not be coerced to a string.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getText

Added in API level 1
CharSequence getText (int index)

Retrieves the styled string value for the attribute at index.

If the attribute is not a string, this method will attempt to coerce it to a string.

Parametersindexint: Index of attribute to retrieve.ReturnsCharSequenceCharSequence holding string data. May be styled. Returns null if the attribute is not defined or could not be coerced to a string.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getTextArray

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

Retrieve the CharSequence[] for the attribute at index. This gets the resource ID of the selected attribute, and uses Resources.getTextArray of the owning Resources object to retrieve its String[].

This method will throw an exception if the attribute is defined but is not a text array resource.

Parametersindexint: Index of attribute to retrieve.ReturnsCharSequence[]CharSequence[] for the attribute, or null if not defined.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getType

Added in API level 21
int getType (int index)

Returns the type of attribute at the specified index.

Parametersindexint: Index of attribute whose type to retrieve.ReturnsintAttribute type.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

getValue

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

Retrieve the raw TypedValue for the attribute at index.

Parametersindexint: Index of attribute to retrieve.outValueTypedValue: TypedValue object in which to place the attribute's data.Returnsbooleantrue if the value was retrieved, false otherwise.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

hasValue

Added in API level 1
boolean hasValue (int index)

Determines whether there is an attribute at index.

Note: If the attribute was set to @empty or @undefined, this method returns false.

Parametersindexint: Index of attribute to retrieve.ReturnsbooleanTrue if the attribute has a value, false otherwise.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

hasValueOrEmpty

Added in API level 22
boolean hasValueOrEmpty (int index)

Determines whether there is an attribute at index, returning true if the attribute was explicitly set to @empty and false only if the attribute was undefined.

Parametersindexint: Index of attribute to retrieve.ReturnsbooleanTrue if the attribute has a value or is empty, false otherwise.ThrowsRuntimeExceptionif the TypedArray has already been recycled.

length

Added in API level 1
int length ()

Returns the number of values in this array.

Returnsint ThrowsRuntimeExceptionif the TypedArray has already been recycled.

peekValue

Added in API level 1
TypedValue peekValue (int index)

Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data. This object is only valid until the next call on toTypedArray.

Parametersindexint: Index of attribute to retrieve.ReturnsTypedValueReturns 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.)ThrowsRuntimeExceptionif the TypedArray has already been recycled.

recycle

Added in API level 1
void recycle ()

Recycles the TypedArray, to be re-used by a later caller. After calling this function you must not ever touch the typed array again.

ThrowsRuntimeExceptionif the TypedArray has already been recycled.

toString

Added in API level 1
String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

ReturnsStringa string representation of the object.
0 0