Steps for customizing controls in Android

Source: Internet
Author: User
Tags xml attribute

Steps for customizing controls in Android
In Android development, it is inevitable that custom controls are required. Some products do not meet the requirements in the Android standard control library, and some do not have code reusable during development, custom.


A good custom control should be the same as the control provided by Android itself. It encapsulates a series of functions for developers. It not only has complete functions, but also needs to use the memory and CPU efficiently. Android provides
Some indicators:
1. the Android standard specifications (such as naming, configuration, and event processing) should be observed ).
2. Configure the properties of the control in the XML layout.
3. appropriate feedback should be given to interactions, such as pressing and clicking.
4. It is compatible with many Android versions and should have extensive applicability.


Android provides a series of basic controls and xml attributes to help you create custom controls.

1. Subclass of View

View is one of the most basic controls in Android. All controls are inherited from View. You can also inherit View directly or other controls, such as ImageView.


Of course, you must provide at least one constructor, where Context and AttributeSet are used as parameters. Example:

class PieChart extends View {    public PieChart(Context context, AttributeSet attrs) {        super(context, attrs);    }}


2. Custom Attributes

A perfect custom control can also add xml to configure attributes and styles. To achieve this, follow these steps:
1) Add Custom Attributes To the xml file
2) in the xml Specifies the attribute value.
3) Get the value in xml in view
4) apply the obtained value to the view.


The following examples are provided:
Add In your program, it is typically stored in the res/values/attrs. xml file, for example:
       
                              
                 
              
       
         
     
    



This Code declares two Custom Attributes showText and labelPosition. They belong to a custom entity PieChat.


Once attributes are defined, they can be used in xml. The following is a simple example:


    
     
     
    

 

It can be seen that, like the standard Android components, the only difference in their belonging to different namespaces, the standard component namespace is generally http://schemas.android.com/apk/res/android,

The custom namespace is http://schemas.android.com/apk/res/~your package name]. Have you noticed that xmlns: m is in custom? You can use any character,
It must be consistent with the characters defined in the following control.
Note that the tag in xml is com. example. customviews. charting. pieChart, the complete package name required. If your custom control is an internal class (well, it's amazing ),
The full path must also be provided. Assume that PieChat has an internal class PieView. If you reference it in XML, you need to use: com. example. customviews. charting. PieChart $ PieView


3) Custom Application attribute values
When a View is created, you can use AttributeSet to read all attributes defined in xml and read attrs through obtainStyledAttributes In the constructor,
This method returns a TypeArray array. You can use TypeArray to read methods that have been defined in XML. The following example shows how to read the xml Attribute Value.

public PieChart(Context context, AttributeSet attrs) {   super(context, attrs);   TypedArray a = context.getTheme().obtainStyledAttributes(        attrs,        R.styleable.PieChart,        0, 0);   try {       mShowText = a.getBoolean(R.styleable.PieChart_showText, false);       mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);   } finally {       a.recycle();   }}


It should be emphasized that after TypeArray is used, it must be destroyed. Otherwise, memory leakage may occur.

4) add custom methods and events
Custom Attributes are powerful, but their disadvantages are also obvious. They can only be applied to controls during view initialization. To add more flexible behaviors, you can add getter and setter for each attribute.
Yes. The following code snippet shows the showText attribute of PieChat.
public boolean isShowText() {   return mShowText;}public void setShowText(boolean showText) {   mShowText = showText;   invalidate();   requestLayout();}


Invalidate () and requestLayout () are called in setShowText to ensure timely update of the view. In your custom View, if a property is changed and needs to take effect immediately,
You must also call this method. In this way, the system will immediately re-draw the view. Similarly, if the size or shape of the view changes, you must also call requestLayout (). Otherwise
Many problems.

Generally, you also need to add an Event Callback to communicate with the caller. For example, PieChat exposes OnCurrentItemChanged to notify the caller that pie chat has been rotated.
During development, it is easy to forget to add some attributes and events, especially when the author is the only user of the custom View. In order to make the View more universal, we should take some time to consider more comprehensive.
You 'd better expose all attributes that may change the appearance and behavior. Of course, this also puts forward higher requirements for you. Otherwise, how can this problem be solved.



















 

Related Article

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.