Android custom control series tutorial-view measurement and Layout

Source: Internet
Author: User

Android custom control series tutorial-view measurement and Layout
As mentioned above, when a view interface is drawn on an android screen, it must go through the following phases: measure, layout, and draw, we can see these functions in the view class, and there are several functions in it: onmeasure, onlayout, ondraw. These functions are the ones we need to pay attention to when rewriting the control, let's talk about the functions and functions of these functions. OnMeasure is a measurement just like the name of this function. In fact, the system does not know how big it is before it is drawn, therefore, in many cases, we cannot directly call getwitfh or getheight of a View when initializing the oncreate interface, because the system has not issued a unified typographical request during oncreate, now, let's take a closer look at what to pay attention to in this onmeasuer function. The parameter he calls back to us is MeasureSpec, which stores the attributes of his father. The value of MeasureSpec is composed of specSize and specMode. specSize records the size and specMode records the specifications. SpecMode has three types:

1. EXACTLY

Indicates that the size of the Child view in the parent view should be determined by the specSize value. The system will set the size of the Child view according to this rule by default, of course, developers can set their own sizes as needed.

2. AT_MOST

It indicates that the child view can only be the size specified in specSize. Developers should set this view as small as possible and ensure that the view does not exceed specSize. By default, the system sets the size of the sub-view according to this rule. Of course, developers can also set the size as they wish.

3. UNSPECIFIED

This means that developers can set the view to any size according to their own wishes without any restrictions. This situation is rare and rarely used.

For more information about measurement, see how LinearLayout and RelativeLayout are implemented to deepen understanding.

OnLayout uses onMeasure to calculate the width and height occupied by this control, so I need to tell the system where it should be placed on the screen in this onlayout function, since basically all the controls are inherited from ViewGroup, let's take a closer look at its onLayout method.
   @Override    protected abstract void onLayout(boolean changed,            int l, int t, int r, int b);
It can be seen that it is an abstract method, so all of our implementation classes need to implement this method, to tell the system how we need to typeset our interface, layout S such as LinearLayout and RelativeLayout overwrite this method, and then deploy the child views internally according to their respective rules. Because the layout rules of LinearLayout and RelativeLayout are complex, they are not analyzed separately.
Here we will use an example to explain what we mentioned above. We will simply implement a vertical layout. Let's take a look at the effect. What we need to implement is a vertical layout container similar to LinearLayout. Now let's look at the Code directly. I will comment out the code in it. We still inherit a ViewGroup according to the rules.
Public class SimpleVertical extends ViewGroup {public SimpleVertical (Context context) {super (context) ;}@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {int totalheight = 0; int childcount = getChildCount (); for (int I = 0; I <childcount; I ++) {View childView = getChildAt (I ); if (childView. getVisibility ()! = GONE) {childView. layout (0, totalheight, childView. getMeasuredWidth (), totalheight + childView. getMeasuredHeight (); // set totalheight + = childView. getMeasuredHeight (); // start of calculation height }}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int childCount = getChildCount (); for (int I = 0; I <childCount; I ++) {View childView = getChildAt (I); if (childView. getVisibility ()! = GONE) {LayoutParams childLp = childView. getLayoutParams (); // get the layout parameter int childWidthMeasureSpec = getChildMeasureSpec (widthMeasureSpec, getPaddingLeft () + getPaddingRight (), childLp. width); // call the VIewGroup encapsulation method int childHeightMeasureSpec = getChildMeasureSpec (widthMeasureSpec, getPaddingTop () + getPaddingBottom (), childLp. height); childView. measure (childWidthMeasureSpec, childHeightMeasureSpec); // The most important thing is to tell yourself the measurement} // measureChildren (widthMeasureSpec, heightMeasureSpec); // you can also call the ViewGroup method super. onMeasure (widthMeasureSpec, heightMeasureSpec); // remember to call this method. Otherwise, an error will be reported. In fact, it is also the setMeasuredDimension method called }}
I have written all the comments above, and then we will write a test Activity. I will paste the code in oncreate directly. It is also very simple
SimpleVertical simpleVertical = new SimpleVertical (this); for (int I = 0; I <12; I ++) {TextView textView = getTextView ("android custom control series tutorial-view measurement and layout"); textView. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, "android custom control series tutorial-view measurement and layout", 0 ). show () ;}}); simpleVertical. addView (textView);} setContentView (simpleVertical );
We can see that we not only wrote our own control, but also added a click event to it. It is no problem to click it, in this way, we have simply implemented the vertical layout of Linearlayotu. To better understand the writing method here, I suggest you look at the writing method of the Linearlayotu and RelaytiveLayout container controls.

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.