Development case of Android AttributeSet API
The two parameters of AttributeSet and Defstyle are often used when constructing view components through XML files.
public class MyButton extends Button{public MyButton (context context, AttributeSet Attrs, int. Defstyle) {Super (context, at TRS, Defstyle);//TODO auto-generated constructor Stubtypedarray a = Context.obtainstyledattributes (Attrs, R.styleable.actionbar, Defstyle, 0);}}
at this point,
The context invokes Obtainstyledattributes (AttributeSet set, int[] attrs, int defstyleattr, int defstyleres) method to obtain a typedarray, The properties of the component are then set according to the Typearray. Obtainstyledattributes this kind of method has several, the real realization is the Resources.theme class, respectively is:
Obtainstyledattributes (AttributeSet set, int[] attrs, int defstyleattr, int defstyleres): TypedArray obtainstyledattributes (int resid, int[] attrs) : Typearray obtainstyledattributes (int[] attrs): Typearray
In the first method, according to Attrs to determine which properties to get, and then sequentially through the remaining 3 parameters to obtain the corresponding property values, the priority of the property value obtained from high to low is set, Defstyleattr, Defstyleres. Defstyleattr is a reference that points to a style in the current theme, a style that is actually a collection of properties, and if Defstyleattr is 0 or if no corresponding style is found in theme, then Before attempting to get the property value from Defstyleres, Defstyleres represents the ID of a style and is invalid when it is 0 o'clock. Methods (2) and (3) respectively indicate that the property value is obtained from the style or theme.
For example:
TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.actionbar, Defstyle, 0);
ATTR is defined under the/res/values/attrs.xml file, and in addition to the properties of the system components themselves, we can also customize the properties and use them in layout layouts. Attrs.xml usually includes several attr collections, such as
<declare-styleable name= "LabelView" > <attr name= "text" format= "string"/> <attr name= " TextColor "format=" color "/> <attr name=" textSize "format=" Dimension "/> </declare-styleable >
Represents a attr collection, the name value in the declare-styleable tag represents the Attrs parameter in the above method, and Android automatically generates an array in the R file, which can make any of the view component names not necessarily. Define the name of each property and its type in the collection, as I see in total there are reference, string, Color, dimension, Boolean, and so on, if multiple types are allowed to use the ' | ' To separate, such as reference | Color, attr can also be defined like this
<attr name= "Layout_height" format= "Dimension" > <enum name= "fill_parent" value= "-1"/> <enum Name= "Match_parent" value= "-1"/> <enum name= "wrap_content" value= "-2"/> </attr>
When the definition of attr does not indicate format, it means that it has already been defined elsewhere, so you can define a attr collection that has already defined properties (such as the properties of the system components). The Obtainstyledattributes method is then used to obtain these property values, such as
<declare-styleable name= "Gallery1" > <attr name= "Android:galleryitembackground"/> </ Declare-styleable>
Of course, we also need to declare our own namespace
xmlns:app= "Http://schemas.android.com/apk/res/your_package_name"
There are two classes of styleable and attr in the R file, which is used when we want to use which property set or property, and the Attr class defines only the ID of the attr attribute in layout. There are two methods of attributeset, namely
int getattributenameresource (int index);
int getattributeresourcevalue (int index, int defaultvalue);
The previous method gets the ID of the Attr property name, which is the value defined by the Attr class, and the latter method gets the value of the Attr property.
For example, if we want to customize Actionbar to adapt to different scenarios
<com.jeason.jeasonactionbar.myactionbarxmlns:bar= "http://schemas.android.com/apk/res/ Com.jeason.jeasonmapraiders "android:layout_height=" @dimen/gd_action_bar_height "android:layout_width=" Fill_ Parent "android:layout_alignparenttop=" true "bar:type=" normal "bar:title=" Campus Map "/>
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 ();}
The corresponding in the Values/attrs.xml file
<declare-styleable name= "ActionBar" ><attr name= "title" format= "string"/><attr name= "type" >< Enum name= "normal" value= "0"/><enum name= "Dashboard" value= "1"/><enum name= "Empty" value= "2"/></ Attr></declare-styleable>
Development case of Android AttributeSet API