Android -- getWidth () and getMeasuredWidth ()

Source: Internet
Author: User

Android -- getWidth () and getMeasuredWidth ()
Return the width of the your view. returns The width of your view, in pixels. source code: public final int getWidth () {return mRight-mLeft;} getwidth returns the right coordinate to reduce the coordinate minus the left coordinate, which can be determined after the layout, that is to say, getwidth can be called only after layout. Therefore, the width obtained by getWidth () is the width of the entire View after the layout is set. GetMeasuredWidth () Return the full width measurement information for this view as computed by the most recent call to measure (int, int ). this result is a bit mask as defined byMEASURED_SIZE_MASK and MEASURED_STATE_TOO_SMALL. this shoshould be used during measurement and layout calculations only. use getWidth () to see how wide a view is after layout. returns The measured width of this view as a bit ma Sk. The last time the measure () method was called to measure the View width, It should be used only for measurement and Layout calculation. Let's look at The source code: public final int getMeasuredWidth () {return mMeasuredWidth & MEASURED_SIZE_MASK;} return The raw measured width of this view to obtain The original measurement width. Therefore, getMeasuredWidth () is the width occupied by the View content after the View content is measured. The premise is that you must call measure () in the onLayout () method of the parent layout or the onDraw () method of this View; (you can define the value of the parameter in measure ), otherwise, you will get the same result as getWidth. Difference getMeasuredWidth: The width occupied by the View content after the content on the View is measured. GetWidth: the width of the View after the layout of the number is set. GetMeasuredWidth: When the custom view overwrites onLayout, we want to get the original width of the view after dynamically loading the view with layoutinflater. GetWidth: it is usually displayed after the view has been laid out. To get the width.

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.