An introduction to the series of Android Elite biography notes

Source: Internet
Author: User
Tags call back

After learning the first line of code, continue to learn the Android Elite biography, hope to persist, study this book well, and timely to do relevant notes, to consolidate relevant knowledge;

A Description of view:

1 '. Let's look at the View tree structure first:

2. Tree structure content:

A.viewparent is the core of the whole tree, which dispatches and distributes all the interactive management events uniformly.

B. Controls are divided into ViewGroup and view two controls, and ViewGroup can contain more than one Vie W control as a parent control.

C. We know that the control is instantiated by Findviewbyid before using the control, so in the tree structure, it is actually represented by the depth-first traversal of the tree to find the corresponding element.

3.UI Interface Frame diagram:

4.UI Interface Frame diagram content:

A. Actually each activity contains a Window object, and the Window object is implemented by Phonewindow.

B.phonewindow also sets a decorview to the root view of the entire application window. (Simply put, we see the entire mobile screen content is the entire decorview);

C. Then in our mobile phone application, generally contains two view, one is the title bar, that is, Titleview, and the other is to render the content of the view, that is, Contentview. (Of course, we can also hide it by some means, such as before Setcontentview ():

Requestwindowfeature (Window.feature_no_title) method. )

D. The display process of the interface: when the program calls the Setcontentview method in the OnCreate () method, Activitymanagerservice will call back the Onresume method, The system adds the entire decorview to the Phonewindow.

Two Measurement of view:

1. There are three types of measurement modes:

Exactly mode: Refers to the exact value mode, such as we set the LAYOUT_WIDTH=30DP, then this is the exact value mode.

At_most mode: Refers to the maximum mode, for example, we use the wrap_content is this mode, allows to change as the content changes, as long as the maximum allowable size of the parent control can be.

Unspecified mode: This is a no-limit mode, how big you want to be, and it is generally used in custom view.

2. About overriding the Onmeasure () method:

A. Why would you want to rewrite the Onmeasure method?

Because the view class default Onmeasure method only supports exactly mode, you must override this method if we want to specify the size of the wrap_content.

B. Rewriting the onmeasure process:

(1) Create a class to inherit to view:

protected void onmeasure (intint  heightmeasurespec) {    super. Onmeasure ( Widthmeasurespec, Heightmeasurespec);}

We'll see an initial onmeasure function like this, and then we'll look at super. Methods of Onmeasure:

protected void onmeasure (intint  heightmeasurespec) {    setmeasureddimension ( Getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec),            getdefaultsize (Getsuggestedminimumheight (), Heightmeasurespec));}

So here we're going to rewrite the setmeasureddimension function: Let's add a sentence like this

Setmeasureddimension (Measuresize (200,widthmeasurespec), Measuresize (200,heightmeasurespec));
Let's take a look at the meaning of this sentence, we actually customize a measuresize function, then pass in two parameters, the first is the default value, and the second actual value. Then let's look at how this custom function is implemented:
Private intMeasuresize (intDefaultSize,intMeasurespec) {    intresult=0;//define result to be returned as a value    intSpecmode=measurespec.getmode (MEASURESPEC);//get to the measurement mode    intSpecsize=measurespec.getsize (MEASURESPEC);//get to measured value    if(specmode==measurespec.exactly) {//if the measurement is accurate, return the measured valueresult=specsize; }Else{result=defaultsize;//if the other two modes are set to the default value        if(Specmode==measurespec.at_most) {//if the maximum mode, we want to get to the default value and the smallest value in the measured valueresult=math.min (result,specsize); }    }    returnresult;}

Finally, we refer to this custom view in the XML file :

< main.view.com.view.viewTest     Android:layout_width = "Wrap_content"     android:layout_height= "Wrap_content"    android:background= "# ff0000 "/>

An introduction to the series of Android Elite biography notes

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.