Measurespec introduction and usage

Source: Internet
Author: User

A measurespec encapsulates the layout requirements that the parent layout passes to the Child layout. Each measurespec represents a set of width and height requirements. A measurespec consists of the size and mode. It has three modes: unspecified (not specified). The parent element forces any constraint on the element, and the child element can get any desired size. Exactly (completely ), the parent element determines the exact size of the Self-element. The child element is limited to a given boundary and its size is ignored. at_most (at most), the child element reaches the specified size at most.

 

It has three common functions:

1. Static
Int getmode (INT measurespec): extract the mode based on the provided measured value (Format) (one of the preceding three modes)

2. Static
Int getsize (INT measurespec): extract the size value based on the provided measured value (Format) (this size is what we usually call the size)

3. Static
Int makemeasurespec (INT size, int mode): Creates a measurement value based on the provided size value and mode (Format)


The usage of this class is usually called in the onmeasure method of the view component, but there are a few exceptions. Let's look at several examples:

 

A. First we will use a useful function, view. resolvesize (int
Size, int measurespec)

 

8421     public static int resolveSize(int size, int measureSpec) {
8422 int result = size;
8423 int specMode = MeasureSpec.getMode(measureSpec);
8424 int specSize = MeasureSpec.getSize(measureSpec);
8425 switch (specMode) {
8426 case MeasureSpec.UNSPECIFIED:
8427 result = size;
8428 break;
8429 case MeasureSpec.AT_MOST:
8430 result = Math.min(size, specSize);
8431 break;
8432 case MeasureSpec.EXACTLY:
8433 result = specSize;
8434 break;
8435 }
8436 return result;
8437 }

Since the measurespec value is used above, it naturally indicates that this function is usually called in the onmeasure method. To put it simply, the main function of this method is to return the desired size value based on the size and mode you provide, which is processed according to the input mode.

Let's take a look at the measurespec. makemeasurespec method. In fact, this method is very simple:

9023         public static int makeMeasureSpec(int size, int mode) {
9024 return size + mode;
9025 }

This makes it easy to understand the difference between size and measurespec. Let's see how it works. listview. measureitem (view child)

 

2464     private void measureItem(View child) {
2465 ViewGroup.LayoutParams p = child.getLayoutParams();
2466 if (p == null) {
2467 p = new ViewGroup.LayoutParams(
2468 ViewGroup.LayoutParams.MATCH_PARENT,
2469 ViewGroup.LayoutParams.WRAP_CONTENT);
2470 }
2471
2472 int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
2473 mListPadding.left + mListPadding.right, p.width);
2474 int lpHeight = p.height;
2475 int childHeightSpec;
2476 if (lpHeight > 0) {
2477 childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
2478 } else {
2479 childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
2480 }
2481 child.measure(childWidthSpec, childHeightSpec);
2482 }

The measurespec method is usually used in viewgroup. It can adjust the size of sub-elements according to the mode (three in measurespec.

Note that using exactly and at_most is usually the same effect. If you want to differentiate them, you need to use the above function view. resolvesize (INT size, int measurespec) returns a size value, and then uses your view to call the setmeasureddimension (INT, INT) function.

 

8406     protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
8407 mMeasuredWidth = measuredWidth;
8408 mMeasuredHeight = measuredHeight;
8409
8410 mPrivateFlags |= MEASURED_DIMENSION_SET;
8411 }

Then you call view. getmeasuredwidth and view. getmeasuredheigth to return the values of mmeasuredwidth and mmeasuredheight in the above function.

  

 

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.