Layout Process of Android View

Source: Internet
Author: User

Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure (INT,
INT) and is a top-down traversal of the view tree. each view pushes dimension specifications down the tree during the recursion. at the end of the measure pass, every view has stored its measurements. the second pass happens in layout (INT,
Int, Int, INT) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

The layout of a view is divided into two steps: The measurement process and the layout process. The measurement process is to use the measure method and traverse the view tree from top to bottom. Each view uses recursion to determine its size parameters on the view tree from top to bottom (this translation feels inappropriate). At the end of the measurement process, each view stores its own size value. The second process starts with the layout () method and is also a process from top to bottom. In this process, the size and position of each parent view should be reasonably arranged based on the size value obtained during the measurement process.

When a view's measure () method returns, its getmeasuredwidth () and getmeasuredheight () Values
Must be set, along with those for all of that view's descendants. A view's measured width and measured height values must respect the constraints imposed by the view's parents. this guarantees that at the end of the measure pass, all parents accept all
Their children's measurements. A parent view may call Measure () more than once on its children. for example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call Measure () on them again with actual
Numbers if the sum of all the children's unconstrained sizes is too big or too small.

When a view's measure () method returns, its getmeasuredwidth () and getmeasuredheight () values must be set successfully (it can be understood as its getmeasuredwidth () and getmeasuredheight () all methods can return values, that is, the measurement of the measure method is successful .), At the same time, the child view of the view has been measured successfully. The width and height of a view must be subject to the size constraints given by its parent view. In this way, all parent views can accept all their child controls after the measurement is complete. A parent view may call Measure () multiple times to measure the size of a child view. For example, the parent view measures the size of its child view for the first time to obtain the actual size of the Child view. (If the size of the Child view is too large or too small, it is too small) the parent view will call measure for the second time to determine the size of the Child view.

The following part is not translated. Many people on the internet translate much better than me, so I won't miss people here. Haha

The measure pass uses two classes to communicate dimensions. The view. measurespec class is used by views
To tell their parents how they want to be measured and positioned. the base layoutparams class just describes how big the view wants to be for both width and height. for each dimension, it can specify one:

  • An exact number
  • Match_parent, which means the view wants to be as big as its parent (minus padding)
  • Wrap_content, which means that the view wants to be just big enough to enclose its content (plus padding ).

There are subclasses of layoutparams for different subclasses of viewgroup. For example, absolutelayout has its own subclass of layoutparams which adds an X and Y value.

Measurespecs are used to push requirements down the tree from parent to child. A measurespec can be in one of three modes:

  • Unspecified: This is used by a parent to determine the desired dimension of a child view. for example, a linearlayout may call Measure () on its child with the height set to unspecified and a width of exactly 240 to find out how
    Tall the child view wants to be given a width of 240 pixels.
  • Exactly: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • At_most: This is used by the parent to impose a maximum size on the child. The child must gurantee that it and all of its descendants will fit within this size.

To intiate a layout, call requestlayout (). This method is typically called by a view on itself when
It believes that is can no longer fit within its current bounds.

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.