A little understanding of Android measure and layout

Source: Internet
Author: User

First, recommend the article, http://blog.csdn.net/hqdoremi/article/details/9980481,http://www.docin.com/p-571954086.html

I understand that measure has 2 functions: one is to invoke the measure function of the child view to generate their height and width so that they can be referenced in their own layout stage. The other is to provide the parent view with the width and height of its own measurements (which is often related to the measurement height and width of the child view) so that the parent view is referenced in the layout stage.

The function of layout is to lay out the size of the sub-view, which can be used to measure the width and height, or not to use (of course, the system comes with the layout is used, because this is the recommended standard practice), It all depends on the layout you want to achieve (for example, I can know the location of the sub-view, you can directly call the sub-view layout function positioning, no measured value), So the Mmeasuredwidth and mmeasuredheight produced by measure are only for reference.

Separating measure from layout makes it easier to reuse system controls. For example, the ListView does not want to use the scroll bar, want to complete the content to be realistic, can simply rewrite a onmeaure function, continue to use the default OnLayout function. The code is as follows:

 public  class   Noscrolllistview extends listview{ public   Noscrolllistview (context context, AttributeSet Attrs) {Super (context, attrs)    ;  public  void  onmeasure (int  Widthmeasurespec,  Heightmeasurespec) { int  mexpandspec = View.MeasureSpec.makeMeasureSpec (integer.max_value >> 2          

Here Onmeaure, modified the parent view of the ListView to the height of the description, to "Up to maximum integer value", in fact, it means that when the parent view layout This listview, the height of the allocation, is not limited by the parent view height (feeling that the ListView scroll bar is based on the size of the parent view in onmeasure), so the ListView will load the content on the parent view, there is no scroll bar, of course, The place beyond the parent view is invisible and needs to be used in conjunction with ScrollView.

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.