public interface AttributeSet
Resources.Theme.obtainStyledAttributes()
which will take care of parsing the attributes for you. In particular,
the Resources API will convert resource references (attribute values such as
"@string/my_label" in the original XML) to the desired type
for you; if you use AttributeSet directly then you will need to manually
check for resource references
(with getAttributeResourceValue(int, int)
) and do the resource
lookup yourself if needed. Direct use of AttributeSet also prevents the
application of themes and styles when retrieving attribute values.
This interface provides an efficient mechanism for retrieving
data from compiled XML files, which can be retrieved for a particular
XmlPullParser through Xml.asAttributeSet()
. Normally this will return an implementation
of the interface that works on top of a generic XmlPullParser, however it
is more useful in conjunction with compiled XML resources:
XmlPullParser parser = resources.getXml(myResouce); AttributeSet attributes = Xml.asAttributeSet(parser);
The implementation returned here, unlike using
the implementation on top of a generic XmlPullParser,
is highly optimized by retrieving pre-computed information that was
generated by aapt when compiling your resources. For example,
the getAttributeFloatValue(int, float)
method returns a floating
point number previous stored in the compiled resource instead of parsing
at runtime the string originally in the XML file.
This interface also provides additional information contained in the
compiled XML resource that is not available in a normal XML file, such
as getAttributeNameResource(int)
which returns the resource
identifier associated with a particular XML attribute name.
Modifier and Type | Method and Description |
---|---|
boolean |
getAttributeBooleanValue(int index,
boolean defaultValue)
Return the boolean value of attribute at 'index'.
|
boolean |
getAttributeBooleanValue(String namespace,
String attribute,
boolean defaultValue)
Return the boolean value of 'attribute'.
|
int |
getAttributeCount()
Returns the number of attributes available in the set.
|
float |
getAttributeFloatValue(int index,
float defaultValue)
Return the float value of attribute at 'index'.
|
float |
getAttributeFloatValue(String namespace,
String attribute,
float defaultValue)
Return the float value of 'attribute'.
|
int |
getAttributeIntValue(int index,
int defaultValue)
Return the integer value of attribute at 'index'.
|
int |
getAttributeIntValue(String namespace,
String attribute,
int defaultValue)
Return the integer value of 'attribute'.
|
int |
getAttributeListValue(int index,
String[] options,
int defaultValue)
Return the index of the value of attribute at 'index' in the list
'options'.
|
int |
getAttributeListValue(String namespace,
String attribute,
String[] options,
int defaultValue)
Return the index of the value of 'attribute' in the list 'options'.
|
String |
getAttributeName(int index)
Returns the name of the specified attribute.
|
int |
getAttributeNameResource(int index)
Return the resource ID associated with the given attribute name.
|
int |
getAttributeResourceValue(int index,
int defaultValue)
Return the value of attribute at 'index' as a resource identifier.
|
int |
getAttributeResourceValue(String namespace,
String attribute,
int defaultValue)
Return the value of 'attribute' as a resource identifier.
|
int |
getAttributeUnsignedIntValue(int index,
int defaultValue)
Return the integer value of attribute at 'index' that is formatted as an
unsigned value.
|
int |
getAttributeUnsignedIntValue(String namespace,
String attribute,
int defaultValue)
Return the boolean value of 'attribute' that is formatted as an
unsigned value.
|
String |
getAttributeValue(int index)
Returns the value of the specified attribute as a string representation.
|
String |
getAttributeValue(String namespace,
String name)
Returns the value of the specified attribute as a string representation.
|
String |
getClassAttribute()
Return the value of the "class" attribute or null if there is not one.
|
String |
getIdAttribute()
Return the value of the "id" attribute or null if there is not one.
|
int |
getIdAttributeResourceValue(int defaultValue)
Return the integer value of the "id" attribute or defaultValue if there
is none.
|
String |
getPositionDescription()
Returns a description of the current position of the attribute set.
|
int |
getStyleAttribute()
Return the value of the "style" attribute or 0 if there is not one.
|
int getAttributeCount()
String getAttributeName(int index)
index
- Index of the desired attribute, 0...count-1.String getAttributeValue(int index)
index
- Index of the desired attribute, 0...count-1.String getAttributeValue(String namespace, String name)
namespace
- The namespace of the attribute to get the value from.name
- The name of the attribute to get the value from.String getPositionDescription()
int getAttributeNameResource(int index)
Note that this is different than getAttributeResourceValue(java.lang.String, java.lang.String, int)
in that it returns a resource identifier for the attribute name; the
other method returns this attribute's value as a resource identifier.
index
- Index of the desired attribute, 0...count-1.int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue)
namespace
- Namespace of attribute to retrieve.attribute
- Name of attribute to retrieve.options
- List of strings whose values we are checking against.defaultValue
- Value returned if attribute doesn't exist or no
match is found.boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue)
namespace
- Namespace of attribute to retrieve.attribute
- The attribute to retrieve.defaultValue
- What to return if the attribute isn't found.int getAttributeResourceValue(String namespace, String attribute, int defaultValue)
Note that this is different than getAttributeNameResource(int)
in that it returns a the value contained in this attribute as a
resource identifier (i.e., a value originally of the form
"@package:type/resource"); the other method returns a resource
identifier that identifies the name of the attribute.
namespace
- Namespace of attribute to retrieve.attribute
- The attribute to retrieve.defaultValue
- What to return if the attribute isn't found.int getAttributeIntValue(String namespace, String attribute, int defaultValue)
namespace
- Namespace of attribute to retrieve.attribute
- The attribute to retrieve.defaultValue
- What to return if the attribute isn't found.int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue)
namespace
- Namespace of attribute to retrieve.attribute
- The attribute to retrieve.defaultValue
- What to return if the attribute isn't found.float getAttributeFloatValue(String namespace, String attribute, float defaultValue)
namespace
- Namespace of attribute to retrieve.attribute
- The attribute to retrieve.defaultValue
- What to return if the attribute isn't found.int getAttributeListValue(int index, String[] options, int defaultValue)
index
- Index of the desired attribute, 0...count-1.options
- List of strings whose values we are checking against.defaultValue
- Value returned if attribute doesn't exist or no
match is found.boolean getAttributeBooleanValue(int index, boolean defaultValue)
index
- Index of the desired attribute, 0...count-1.defaultValue
- What to return if the attribute isn't found.int getAttributeResourceValue(int index, int defaultValue)
Note that this is different than getAttributeNameResource(int)
in that it returns a the value contained in this attribute as a
resource identifier (i.e., a value originally of the form
"@package:type/resource"); the other method returns a resource
identifier that identifies the name of the attribute.
index
- Index of the desired attribute, 0...count-1.defaultValue
- What to return if the attribute isn't found.int getAttributeIntValue(int index, int defaultValue)
index
- Index of the desired attribute, 0...count-1.defaultValue
- What to return if the attribute isn't found.int getAttributeUnsignedIntValue(int index, int defaultValue)
index
- Index of the desired attribute, 0...count-1.defaultValue
- What to return if the attribute isn't found.float getAttributeFloatValue(int index, float defaultValue)
index
- Index of the desired attribute, 0...count-1.defaultValue
- What to return if the attribute isn't found.String getIdAttribute()
String getClassAttribute()
int getIdAttributeResourceValue(int defaultValue)
defaultValue
- What to return if the "id" attribute isn't found.int getStyleAttribute()