Android advanced exercise-custom view (1)

Source: Internet
Author: User
Custom View
Although the android framework provides a lot of interaction with users and can display views of various data, sometimes the basic view controls built in Android cannot meet some special requirements, in this case, we need to customize the view control. Create a custom View class
A well-designed custom view is like a well-designed class. It encapsulates a specific set of easy-to-use methods, in addition to a well-designed class, a custom view should meet the following conditions:
1. comply with Android platform standards 2. Use Android XML layouts to provide custom style attributes 3. Send accessible events 4. Be compatible with multiple Android platforms
The android framework provides us with a set of basic classes and XML labels to help us create a View class that meets the preceding requirements, now let's learn how to use the android framework to implement the core function of a view class. How to create a custom View class
All view classes in the android framework inherit from the View class. Our custom view classes can also directly inherit the View class, of course, if we want to save time, we can also inherit from a subclass of the implemented view. For example, the button class allows our view to interact with the Android Developers tool, our custom View class must provide Attributeset is a parameter constructor. Through this constructor, our view controls allow the layout editor to construct and set styles through resource files, this allows us to edit in the graphic editor provided by Android.
class PieChart extends View {    public PieChart(Context ctx, AttributeSet attrs) {        super(ctx, attrs);    }}

Custom Attributes
We add a built-in View to the user interface. We need to specify it in an XML Element file and control its display and behavior through element attributes, to better implement a custom view, we also need to use XML to add it to the user interface and define its style. To meet this requirement, we should do the following:
1. In the resource element   <Declare-styleable>  Define custom properties 2. Specify the property value in the XML layout file 3. Search for the property value at runtime 4. Apply the retrieved property value in the view

In Res/values/attrs. xmlUse <Declare-styleable>Element-defined custom attributes
<resources>;   <declare-styleable name="PieChart">       <attr name="showText" format="boolean" />       <attr name="labelPosition" format="enum">           <enum name="left" value="0"/>           <enum name="right" value="1"/>       </attr> 
   </declare-styleable>
</resources> 

Specify the custom attribute value in XML layout.
<?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.example.customviews"> <com.example.customviews.charting.PieChart     custom:showText="true"     custom:labelPosition="left" />
</LinearLayout>

When we add a custom view control to layout, We need to specify the class full name <packagename. classname>. If our view class is an internal class, it is <packagename. outclassname $ innerclassname>
Use Custom Attributes
When your view is created in XML layout, all configured attributes can be read from the resource file, they will be passed to the view constructor as attributeset parameters. Although the attribute values can be directly read from attributeset, this method has some disadvantages: 1. The problem of referencing attribute values in the resource file is not solved. 2. The style has not been attached. obtainStyledAttributes()  MethodTo read the attribute value.  Obtainstyledattributes ()  Method will have a typedarray
Type return value. The attribute values stored in the typedarray are referenced and appended.
public PieChart(Context ctx, AttributeSet attrs) {   super(ctx, 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();   }}
Expose attributes and add event listeningAttribute is a powerful means to control the appearance and behavior of a view, but they can be obtained only after the view Initialization is complete. In order to provide a dynamic means, provides the setter and getter methods for each custom property.
public boolean isShowText() {   return mShowText;}public void setShowText(boolean showText) {   mShowText = showText;   invalidate();   requestLayout();}
Changes to any attribute value may affect the appearance and data of the View Interface. We need to call the invalidate () method to notify the system to re-draw the view interface. Similarly, changing the attribute value may also affect the size and shape of the View Interface. We need to call requestlayout ()
To regenerate the layout of the View Interface.
We also need to set some listeners to monitor important events, such as the user's touch screen events...
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.