Android developer View rewrite related Api-onlayout,onmeasure,measurespec

Source: Internet
Author: User

1.onLayout

Android.view.ViewGroup

protected void OnLayout (Boolean changed, int l, int t, int r, int b)

The OnLayout method is called when the layout operation is performed. View to set size and position for each child of it. Subclasses with children need to override the OnLayout method and invoke the layout method of each child.

The parameter changed indicates that the size or position of the view has changed. The parameters L, T, R, and B represent the left, top, right, and bottom position, respectively, relative to the parent.

2.onMeasure

protected void onmeasure (int widthmeasurespec, int heightmeasurespec)

Measure the view and its content to determine measuredwidth and measuredheight. Called in method measure (int, int). When overriding the Onmeasure method, you need to call method setmeasureddimension (int, int) to store the view's Measuredwidth and measuredheight. If the store fails, the method measure (int, int) throws an exception illegalstateexception. You can call the super.onmeasure (int, int) method.

Unless Measurespec permits a larger size, the default implementation of measure is background size. Subclasses rewrite onmeasure (int, int) to provide a better measure of content. If Onmeasure is overridden, subclasses must ensure that Measuredwidth and measuredheight are at least the minheight and minwidth of the view. Minheight/width is obtained through Getsuggestedminimumhight/width ().

The parameter Width/heightmeasurespec represents the horizontal/vertical space requirement imposed by the parent.

void Android.view.ViewGroup.layout (int l, int t, int r, int b)

Set size and position for view and its descendants. It is the 2nd step of the layout mechanism, where each parent invokes its Chlidren layout operation. Use the size and position data measured in the measure phase to complete the layout operation. Subclasses that have child must override the OnLayout method to invoke the layout action of each children.

void Android.view.ViewGroup.measureChildren (int widthmeasurespec, int heightmeasurespec)

Please check all children of the view to measure Themseles, based on the view Measurespec and padding. Ignores children in the GONE state, whether the GONE state is determined by Getchildmeasurespec.

The parameter width/heightmeasurespec represents the width/height requirement for the view.

3.MeasureSpec

Android.view.View.MeasureSpec

Measurespec is the inner class of view

public static Class Measurespec

Measurespec encapsulates the layout requirements passed from parent to child. Each MEASURESPEC represents a requirement for width/height. The Measurespec is made up of size and mode. There are 3 types of mode available:

1. UNSPECIFIED indicates that the parent did not impose any constraint on the child.

2. exactly indicates that the parent has determined the exact size of the child.

3. At_most indicates that child can be set to any value within the specified size.

The MEASURESPEC is implemented as an int type, which reduces the allocation compared to the object type. You can <size, mode>, and unpack as int types.

The constants defined by MEASURESPEC are:

private static final int mode_shift = 30;

private static final int mode_mask = 0x3 << mode_shift;

public static final int UNSPECIFIED = 0 << mode_shift;

public static final int exactly = 1 << mode_shift;

public static final int at_most = 2 << mode_shift;

The methods defined by MEASURESPEC are:

public static String toString (int measurespec) {
int mode = GetMode (MEASURESPEC);
int size = GetSize (MEASURESPEC);

StringBuilder sb = new StringBuilder ("Measurespec:");

if (mode = = UNSPECIFIED)
Sb.append ("UNSPECIFIED");
else if (mode = = exactly)
Sb.append ("exactly");
else if (mode = = At_most)
Sb.append ("At_most");
Else
Sb.append (Mode). Append ("");

Sb.append (size);
return sb.tostring ();
}

public static int GetSize (int measurespec) {
Return (Measurespec & ~mode_mask);
}

public static int GetMode (int measurespec) {
Return (Measurespec & Mode_mask);
}

public static int Makemeasurespec (int size, int mode) { 
    return size + mode; 
}

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.