Android Layout Basics

Source: Internet
Author: User

What the layout process means

The layout process is the process by which the program uses the code of the layout file to calculate the actual size at run time.

What the layout process does

Two stages: The measurement phase and the layout stage.

Measurement Phase : Recursively invokes each view or ViewGroup's measure () method from top to bottom to measure their size and calculate their position; layout Stage : Recursively call each view or ViewGroup from top to bottom Layout () method to assign the measured size and position to them.

Layout process for View or ViewGroup
  1. Measurement phase, measure ()   method called by parent View, after doing some preparation and optimization work in   measure ()  , call   onmeasure ()   for actual self-measurement.   onmeasure ()   do, View   and   viewgroup   different:

    1. View : view   in   onmeasure ()   calculates its size and then saves it;
    2. viewgroup : viewgroup   in   onmeasure ()   calls all child View &NBSP, measure ()   Let them measure themselves and calculate their actual size and position based on the desired dimensions computed by the Sub view (in fact, 99.99% of the parent view uses the child view The desired size is given as the actual size, as explained in the next or next period) and then saved. It also calculates its size and saves it according to the size and position of the child View;
  2. In the layout phase, the layout() method is called by the parent view, where layout() it saves the parent view's own location and dimensions, and calls onLayout() to make the actual internal layout. onLayout()do things, View and ViewGroup also not the same:

    1. View: Because there is no child view, so do View onLayout() nothing.
    2. viewgroup: ViewGroup in onLayout() will call all of their own child View layout() methods, their size and position to them, let them complete their own internal layout.
How the layout process is customized

Three categories:

    1. Rewrite onMeasure() to modify an existing View dimension;
    2. Rewrite onMeasure() to customize View the size of the new custom;
    3. Rewrite onMeasure() and customize onLayout() ViewGroup the internal layout to new customizations.
Specific practices for the first type of customization

That is, rewrite onMeasure() to modify the existing View size of the specific practice:

    1. Override the onMeasure() method and invoke it inside super.onMeasure() , triggering the original self-measurement;
    2. In super.onMeasure() the following use getMeasuredWidth() and getMeasuredHeight() to obtain the previous measurement results, and use their own algorithm, according to the measurement results calculated new results;
    3. Called setMeasuredDimension() to save the new result.
New custom customizations for the second typeViewThe size
    • The most important difference between a new custom size and a modified size

It is necessary to ensure that the calculation result satisfies the size limit given by the parent View while calculating

    • Size limit for parent View
    1. Origin: The requirements of the developer (the layout_ attributes that begin in the layout file) are more accurate after the parent View processing is calculated;
    2. Classification of restrictions:
      1. UNSPECIFIED: No Limit
      2. AT_MOST: Limit Caps
      3. EXACTLY: Limit Fixed values
    • New ways to define custom View dimensions
      1. Again onMeasure() , and calculate the size of the View;
      2. Use resolveSize() to have the child view's calculations conform to the limits of the parent view (of course, if you want to satisfy the limits of the parent view in your own way).
The third type of way to customize layout interior layouts
    1. Override onMeasure() to calculate internal layout
    2. Rewrite onLayout() to place child View
    • Three steps to rewrite Onmeasure ():
    1. Call each child view measure() to calculate the dimensions of the child view
    2. Calculates the location of the child view and saves the location and size of the child view
    3. Calculate your own size and setMeasuredDimension() save
    • Key to calculating sub-View dimensions

The key to calculating the size of a sub-view is the two parameters of the method-that is, the measure() two calculation of the child view MeasureSpec .

    • The measurespec of the child View is calculated as follows:
    • Combine the developer's requirements (the attributes that begin in the XML layout_ ) and your own free space (your own size limit-used size)
    • The size limit is based on your own MeasureSpec mode.
      • Exactly/at_most: The upper limit for MeasureSpec the size ofsize
      • UNSPECIFIED: No limit on size
    • How to override OnLayout ()

onLayout()call each sub-View in layout() , and let them save their location and size.

Android Layout Basics

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.