Android AttributeSet API development case
Android AttributeSet API development case when constructing a view component through an xml file, the AttributeSet and defStyle parameters are usually used.
public class myButton extends Button{public myButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubTypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, defStyle, 0);}}
At this time,
Context calls the obtainStyledAttributes (AttributeSet set, int [] attrs, int defStyleAttr, int defStyleRes) method to obtain a TypedArray, and then sets the component attributes based on this TypeArray. There are several methods like obtainStyledAttributes. The real implementation is the Resources. Theme class, which are:
obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) : TypedArray obtainStyledAttributes( int resid, int[] attrs) : TypeArray obtainStyledAttributes(int[] attrs) : TypeArray
In the first method, the attributes to be obtained are determined based on attrs, and then the corresponding attribute values are obtained through the other three parameters in sequence. The priority obtained from the attribute values is set, defStyleAttr, defStyleRes. defStyleAttr is a reference that points to a style in the current Theme. style is actually a collection of various attributes. If defStyleAttr is 0 or the corresponding style is not found in Theme, then, the system will try to get the attribute value from defStyleRes. defStyleRes indicates the id of a style, which is invalid when it is 0. Methods (2) and (3) indicate obtaining attribute values from style or Theme.
For example:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, defStyle, 0);
Attr is defined in the/res/values/attrs. xml file. In addition to the system component attributes, you can also customize attributes and use them in layout. Attrs. xml usually contains several attr sets, such
It indicates an attr set. The name value in the declare-styleable label indicates the attrs parameter in the above method. android will automatically generate an array in the R file, it makes any view component name unnecessary. Define the name and type of each attribute in the set. We can see a total of reference, string, color, dimension, boolean, etc, multiple types can be separated by "|", such as reference | color and attr.
If the format is not specified in the definition of attr, it indicates that it has been defined elsewhere, so you can define an attr set, all the defined attributes (for example, system component attributes) are included, and these attributes are obtained through the obtainStyledAttributes method, for example
Of course, we also need to declare our namespace
xmlns:app="http://schemas.android.com/apk/res/your_package_name"
The R file contains the styleable and attr classes. We use styleable when we want to use a set of attributes or attributes, the attr class only defines the id of the attr attribute in layout. attributeSet has two methods:
Int getAttributeNameResource (int index );
Int getAttributeResourceValue (int index, int defaultValue );
The previous method obtains the id of the attr attribute name, that is, the value defined by the attr class. The latter method obtains the attr attribute value.
For example, if we want to customize the ActionBar to adapt to different scenarios
public class MyActionBar extends LinearLayout {private CharSequence mTitle;private MyActionBar.Type mType; public enum Type {Normal,Dashboard,Dashboard}ublic MyActionBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, defStyle, 0);mTitle = a.getString(R.styleable.ActionBar_title);int layoutID;int type = a.getInteger(R.styleable.ActionBar_type, -1);switch (type) {case 2:mType = Type.Empty;layoutID = R.layout.gd_action_bar_empty;break;case 1:mType = Type.Dashboard;layoutID = R.layout.gd_action_bar_dashboard;break;case 0:default:mType = Type.Normal;layoutID = R.layout.gd_action_bar_normal;break;}LayoutInflater.from(context).inflate(layoutID, this);a.recycle();}
In the values/attrs. xml file