Sometimes in order to complete some functions, you need to combine some system controls, for later use, you can separate this function into a separate control, the use of some parameters can be set the properties of the control, set in the XML, the steps are as follows:
1. Create a custom control Extendsview extends View
2. Define a Attrs.xml file under the values file
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <resources> 3 < Declare-styleable name= "extendsview" > 4 <attr name= "myattr" format= "Boolean"/> 5 </declare-styleable> 6 </resources>
3. Get the property value in the constructor of the custom control
1 PublicExtendsview (Context context,attributeset attrs)2 { 3 Super(Context,attrs); 4 5TypedArray A =context.obtainstyledattributes (Attrs,6 r.styleable. Extendsview); 7 8 BooleanMyAttr =A.getcolor (r.styleable. Extendsview_myattr,9 false); Ten One a.recycle (); A}
4. Use in XML, root layout add xmlns:app= "Http://schemas.android.com/apk/res-auto"
<view app:myattr= "true"/>
Custom control's property definition and use