TypedArray class gets attributes

AttributeSet
AttributeSet is a collection of element attributes in the xml file. It provides various APIs for us to obtain attribute values ​​from compiled xml files, such as getAttributeIntValue, getAttributeBooleanValue, getAttributeFloatValue, etc., which will return the corresponding type of attribute value. The incoming parameters generally have two forms, as follows:

getAttributeXXXValue (int index, XXX defaultValue): Get the corresponding attribute value according to the index of the corresponding attribute. The index value range is between 0 ~ count-1, and the defaultValue cannot be returned.
getAttributeXXXValue (String namespace, String attribute, XXX defaultValue): Get the corresponding attribute value according to the attribute name of the specified namespace

However, we rarely use the methods provided by the AttributeSet class to get its attributes, often through the Theme.obtainStyledAttributes method to get the attributes.

why? ? Example using Attribute in xml file to get attributes

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="@dimen/w_50dp"
  android:layout_height="@dimen/w_50dp"
  android:orientation="vertical"/>

If you use AttributeSet directly to get the attribute value, it has the following disadvantages:

final XmlResourceParser parser = getResources().getLayout(R.layout.ll);
int type;
while ((type = parser.getEventType()) != XmlPullParser.START_TAG
    && type != XmlPullParser.END_DOCUMENT) {
  parser.next();
}
int count = parser.getAttributeCount();
Log.d(TAG, "count: " + parser.getAttributeCount());
AttributeSet attributeSet = Xml.asAttributeSet(parser);
for (int i = 0; i < count; i++) {
  String name = attributeSet.getAttributeName(i);
  String value = attributeSet.getAttributeValue(i);
  Log.d(TAG, "attribute: " + name + "   value: " + value);
}
int resourceId = attributeSet.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "layout_height", 0);
float dimension = getResources().getDimension(resourceId);
Log.d(TAG, "layout_height: " + dimension);

Obtaining element attributes through index index is quite troublesome, and it is difficult for people who are not familiar with the order of attributes to match index with attribute values ​​one by one; while obtaining through the namespace, you must fill in the Android namespace "http: //schemas.android.com/apk/res/android ", it is not very convenient to write.
For" @ String / hello_world "this kind of attribute value is obtained by indexing the resource file, and only the resource is obtained directly by getAttributeValue id, although you can get the id through getAttributeResourceValue and get the corresponding attribute value through the Resources class, but there will be multiple steps of operation.

Let's take a look at how to get the property value using TypedArray. TypedArray is a container for storing attribute value arrays. Usually, TypedArray is obtained as follows:

Resources.Theme.obtainStyledAttributes(AttributeSet set,
	@StyleableRes int[] attrs, 
	@AttrRes int defStyleAttr, 
	@StyleRes int defStyleRes)

AttributeSet set: indicates that the attribute is selected from the AttributeSet, which can be empty
int [] attrs: indicates the attribute you want to select, which attributes you want to get, you can write it into this int array
int defStyleAttr: indicates that the attribute is selected from defStyleAttr , Can be empty
int defStyleRes: indicates that select attributes from defStyleRes, can be empty

The specific use is as follows

TypedArray a = obtainStyledAttributes(attributeSet,
        new int[] { android.R.attr.layout_width, android.R.attr.layout_height});
float width = a.getDimension(0, 0f); //第一个参数表示在int数组中的索引,第二个参数为默认值
Log.d(TAG, "typedarray layout_width: " + width);
float height = a.getDimension(1, 0f); //第一个参数表示在int数组中的索引,第二个参数为默认值
Log.d(TAG, "typedarray layout_width: " + height);

TypedArray provides various APIs, such as getInteger, getString, getDimension, and other methods to obtain attribute values. These methods require the position index of the int array corresponding to the attribute name in obtainStyledAttributes.

Published 16 original articles · Likes0 · Visits 854

Guess you like

Origin blog.csdn.net/weixin_45830683/article/details/103016202