Implementation method:
1. Inherit from ViewGroup or layout, customize settings sub-view location, size, etc., used to assemble some components, resulting in a composite component
2. Inherit from the widget View that extends the functionality of existing components
3. Inherit from view, fully customize a component
Constructors for custom classes:
Public CustomView2 (Context context) {//when called directly in code, use the function super (context);} Public CustomView2 (context context, AttributeSet attrs) {//Use this function super (context, attrs) when using a custom view in XML;} Public CustomView2 (context context, AttributeSet attrs, int defstyle) {//can be manually called by the previous function in the super (context, attrs, Defstyle);}
The attrs in the custom function represents the set of properties for the view, Defstyle represents the default property resource set ID
The process of using a custom view in XML:
Custom Attribute Definition Properties Res/values/attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "test" format= "integer"/> <dec Lare-styleable name= "CustomView" > <attr name= "Test"/> <!--reuse declaration on external properties definition test---< attr name= "atr1" format= "Reference"/> <!--reference to a resource such as @drawable/img--> <attr name= "atr2" format= "string" /> <!--property is String type--<attr name= "ATR3" format= "String|reference"/> <!--string type or reference-- <attr name= "ATR4" format= "boolean"/> <!--Boolean true false--<attr name= "ATR5" format= "integer "/> <!--integers--<attr name=" ATR6 "format=" float "/> <!--float-to <attr name=" ATR7 "fo rmat= "Color"/> <!--color values #rgb #rrggbb #argb #aarrggbb--<attr name= "Atr8" format= "Dimension"/> < ;! --Dimension value--<attr name= "ATR9" format= "fraction"/> <!--percent--<attr name= "Atr10" > <! --enum-- <enum name= "Spring" value= "1"/> <enum name= "Summer" value= "2"/> </attr> < ; attr name= "art11" > <!--bit or operation means Spring|summber--<flag name= "Spring" value= "4"/> <flag name= "Summer" value= "8"/> </attr> </declare-styleable> </resources>
Use in layouts
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:custom=" Http://schemas.android.com/apk/res/com.stone "android:layout_width=" Match_parent "Android: layout_height= "match_parent" android:orientation= "vertical" > <!--xmlns:android The default namespace means that all properties starting with Android are in a The Ndroid package found Xmlns:custom properties defined under the Packagecom.stone package-all properties in <declare-styleable/>-<linearlayout Android:layout_width= "150DP" android:layout_height= "150DP" > <!--Using Custom view--<com.ston E.view.customview1 android:layout_width= "wrap_content" android:layout_height= "Fill_parent" Custom:score= "60%" custom:rotation= " -45" custom:color= "#3f00ff00"/> <!--Wrap _content, then the width is not specified, corresponding to the measurement mode rule measurespec.at_most fill_parent Specifies the height as the parent view measurespec.exactly-< /linearlayout> <!--using a custom VIew--<com.stone.view.customview2 android:layout_width= "match_parent" android:layout_height= "wrap _content "/> <!--when a custom view is an inner class, you need to set the class property with <view as follows/>--<view android:layout_width = "Wrap_content" android:layout_height= "wrap_content" class= "com.stone.view.customview1$custom1"/> < ;/linearlayout>
Resolving custom properties in code
Public CustomView1 (context context, AttributeSet Attrs) {Super (context, attrs);//atts includes typedarray array = Context.obtainstyledattributes (Attrs, r.styleable.customview1);//The system will precede the custom attribute with the Name_int of the declare-styleable it belongs to color = Array.getcolor (R.styleable.customview1_color, color.white); float rotation = array.getfloat ( R.styleable.customview1_rotation, 0f); float score = array.getfraction (R.styleable.customview1_score, 0, 13, 10); Array.recycle ();//Recycle System.out.println ("color=" + Color + ", rotation=" + rotation + ", score=" + score); Setbackgroundcolo R (color);}