Details about Android getwidth and getmeasuredwidth

Source: Internet
Author: User
Google Documents:

Getwidth ():

Return the width of the your view.

Returns:The width of your view, in pixels

Getmeasuredwidth ():

The width of this view as measured in the most recent call to measure (). 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

Prerequisites:

1. During class initialization, the actual size of the view is not obtained in the constructor (I have tested it, indeed ). You can try it. The getwidth () and getmeasuredwidth () results are both 0, but they can be obtained from the ondraw () method or the dispatchdraw () method. You can call invalidate () to execute the ondraw () and dispatchdraw () methods.

2. The unit of the results obtained by these two methods is pixel.

Correct understanding:

Getwidth (): the width of the entire view after the layout is set.

Getmeasuredwidth (): The width occupied by the View content after the content on the view 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; (the value of the Measure parameter can be defined by a friend ), otherwise, the result obtained is the same as that obtained by getwidth.

The main difference between the two methods is whether the measure () method is used, and the position used by measure () is also important.

The difference between getheight () and get measuredheight () is the same.

Refer:[Original] Positive Solutions for Android getwidth and getmeasuredwidth

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.