Custom control's property definition and use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.