Android Reading Note 3: Control schemas and custom controls

Source: Internet
Author: User

How are so many colorful controls on the Android platform made? There are system customizations, as well as developers who are customized on a system basis. But they must all follow certain rules, that is, Android's architectural design for controls.

To be concise:
1, all the controls have a common parent class, either the parent class is a view, or the parent class is ViewGroup, as the name implies, the latter means a group of view, the former is a single control.
There is a concept called the control tree, that is, all the controls if they are drawn into a structure, it must be a tree structure, and the Findviewbyid in our activity is to find the corresponding view according to the depth-first traversal of the tree (from this point of view, if the XML is too complex to write, May affect the efficiency of the Findviewbyid).
View must be within the ViewGroup, ViewGroup for its internal view of the unified scheduling and distribution, control the entire view effect.

This is a component tree inside the androidstudio that represents a structure diagram of all the components inside a layout.xml.

2, for the activity of the code should be not unfamiliar, within the OnCreate method, after the call to Setcontentview, activity content will be drawn out. And the first step in drawing is to measure measure. After each component is created, it is assigned a rectangular area, which is used as its own area for drawing views. So how big is this rectangular area and where is it?
The size of the rectangle, is a method by the view interior: Onmeasure ()

This is a comment for view:onmeasure. The meaning is:
If the method quilt class overrides, then the subclass has the responsibility to ensure that the measured width is at least the minimum width of the view, how to understand, see the following code.

@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {log.d ("MyTag", "onmeasure");    Setmeasureddimension (Mymeasure (Widthmeasurespec), Mymeasure (Heightmeasurespec, 100));     }/** * If you write a custom component, this code can basically be used as a template for Measure * * @param measurespec * @param defaultvalue * @return        */private int mymeasure (int measurespec, int defaultvalue) {int result = 0;        int specmode = Measurespec.getmode (Measurespec);        int specsize = measurespec.getsize (Measurespec);        Precise mode.        If within the XML, the width/height of the component is defined, the combination of the specific number + units is used.        Then it is the exact pattern, because the width/height of the component is explicitly specified in the XML.        if (Specmode = = measurespec.exactly) {result = Specsize;            } else {//Then the above says, if the system specifies that the width/height is wrap_content,//That this is the At_most mode, you must assign it a default value defaultvalue here, so that the code is optimized,            If you specify wrap_content, the system does not know how much space to allocate to it, so the default of the component's wide/high full parent component, this is obviously unreasonable, wasting resources.  So here is a default maximum value, even if the component does not have content, the system also knows how much space should be given to him, without causing waste          if (Specmode = = measurespec.at_most) {result = Math.min (defaultvalue, specsize);//Get here Specsize its    It is the size of the parent component, so take the small} return result; }

  

Android Reading Note 3: Control schemas and custom controls

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.