The attrs.xml is typically created under a resource file
1.attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <declare-styleable name= "button" > <attr name= "textSize" format= "Dimension"/> </declare-styleable> </resources> If the root tag is a resources, define the variable to have a name, declare-styleable name= "button", where the name is the button. In this name, you can have more than one custom attribute. A property named Textsize is defined, and the format is dimension, which specifies the type of the Textsize property and can only be used to define the font size.
2. The layout file is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:myapp= "Http://schemas.android.com/apk/res/com.easymorse.textbutton"android:orientation= "vertical" android:layout_width= "fill_parent"Android:layout_height= "Fill_parent" android:background= "@drawable/background_color" > <linearlayout android:layout_width= "Fill_ ParentAndroid:layout_height= "10dip"/> <linearlayout android:layout_width= "Fill_parent"Android:layout_height= "40dip" > <Com.easymorse.textbutton.TextButton android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"Android:layout_weight= "1" android:text= "movie"android:gravity= "Center_vertical|center_horizontal"Android:background= "@drawable/button" android:focusable= "true"android:clickable= "true" myapp:textsize= "20sp"/>One more xmlns:myappmyapp:textSize in the root tag.= "20SP"
3. Get the value under the program
Public Textbutton (final context context, AttributeSet attrs) { This (context, attrs,0); TypedArray TypedArray=context.obtainstyledattributes (attrs, R.styleable.button); this. Settextsize (Typedarray.getdimension (R.styleable.button_textsize,)); Typedarray.recycle (); TypedArray and Obtainstyledattributes (AttributeSet Attrs,r.styleable.button)
4.
Here are the custom properties of format, there can be many kinds: referencestringcolordimension Boolean integer float Fraction enum Flag
The role of Typearray and Obtainstyledattribute