Difference between getwidth and getmeasuredwidth in Android

Source: Internet
Author: User

After reading the SDK today, I am not very familiar with this things. Google may gain some benefits after a while. The following is my understanding.

Getwidth indicates the actual size of a view.

Getmeasuredwidth indicates the size of a view in the parent view.


What is the usage?

  1. Getwidth is 0 in oncreat .. it is strange that I found the cause: When a view object is created, Android does not know its size, so the getwidth () and getheight () returns 0, the actual size is calculated only when the layout is calculated. Therefore, it is interesting to find that ondraw () can achieve the length and width. So how can we get it at oncreat?

Width = activity. getwindowmanager (). getdefadisplay display (). getwidth ();
Height = activity. getwindowmanager (). getdefadisplay display (). getheight ();


2. getmeasuredwidth must be obtained after the parent view or the measure () function is called by itself. The measure function is used to calculate the space required by the 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.