Custom property methods for custom view

Source: Internet
Author: User

General custom view to achieve some effect, the carbon OnDraw () method is OK. When we need to dynamically respond to user actions and to manipulate properties directly like Android comes with a view, we can use the custom View property method to dynamically respond to our custom view for this purpose.

In order to implement custom view custom properties, you should complete these four steps:

    • Define your own properties for your view under the Resources tab
    • Specifying attribute values in your XML layout
    • Get property values at run time
    • Apply the acquired attribute value to your view

Step1:

Define your own properties under the Resource tab and place them in the res/values/attrs.xml file. The following is an example of a attrs.xml file:

1 <Resources>2    <declare-styleablename= "Piechart">3        <attrname= "Showtext"format= "Boolean" />4        <attrname= "LabelPosition"format= "enum">5            <enumname= "Left"value= "0"/>6            <enumname= "Right"value= "1"/>7        </attr>8    </declare-styleable>9 </Resources>

The above code declares 2 custom properties,showtext and labelposition, that are owned by the Styleable instance under Piechart's project. The name of the styleable instance, usually consistent with the custom view name. Of course you can not agree.

Step2:

Once you have defined your own properties, you can use them in the layout XML file. The only difference is that your own property is the namespace that belongs to your project. Namespaces that are not part of the http://schemas.android.com/apk/res/android, they belong to the Http://schemas.android.com/apk/res/[your package name]. For example, the following shows how to use the properties defined above for Piechart:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:custom= "Http://schemas.android.com/apk/res/com.example.customviews">4  <Com.example.customviews.charting.PieChart5      Custom:showtext= "true"6 custom:labelposition= "Left" />7 </LinearLayout>

to avoid entering long strings of namespace names, the example above refers to the method of Android aliases, using custom as the alias, you can also choose the other name for your namespace, this is just a variable name. Note that if your view is an internal class, you must specify the external class for the view. Similarly, if Piechart has an inner class called Pieview. In order to use the properties that are set in this class, you should use Com.example.customviews.charting.piechart$pieview.

Step3:

Gets the property value at run time, when view is created from XML layout, the attribute value under the XML tag is read from resource and passed to the view's constructor as a attributeset parameter. We get the property value by Obtainstyledattributes (). This method passes a Typedarray object. View resolves the property values through the Typedarray object.

1  PublicPiechart (Context context, AttributeSet attrs) {2    Super(context, attrs);3TypedArray A =context.gettheme (). Obtainstyledattributes (4 Attrs,5 R.styleable.piechart,60, 0);7 8    Try {9Mshowtext = A.getboolean (R.styleable.piechart_showtext,false);TenMtextpos = A.getinteger (r.styleable.piechart_labelposition, 0); One}finally { A a.recycle (); -    } -}

PS: Note that the Typedarray object is a public resource and must be recycled after it is used.

STEP4:

By getting the custom attribute from the construction method, we can apply the custom attribute. to dynamically implement some view effects, we can release getter and setter methods to achieve the goal:

1  Public BooleanIsshowtext () {2    returnMshowtext;3 }4 5  Public voidSetshowtext (BooleanShowtext) {6Mshowtext =Showtext;7 invalidate ();8 requestlayout ();9}

Notice that in the Setshowtext method there is a call to invalidate ()) and requestlayout ()). When some content of the view changes, call invalidate to notify the system to redraw the view, and call the Requestlayout method when some element changes will cause the component size to change.

Custom property methods for custom view

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.