Hackthirteen obtains the width and height of the view in the oncreate () method.

Source: Internet
Author: User

1. Summary:

Many modules in the android source code use the post () method. It is important to thoroughly understand the framework's previous running mechanism to avoid small traps similar to the one in this example. question: If you develop features that depend on the width and height of the UI control, developers may use the getheight () and getwidth () Methods of view. Try to get the width and height of the control in the oncreate () method of activity. Unfortunately, if the developer calls the above method in the oncreate () method, it will find that the return value is 0. 3. cause: When the oncreate () method is called, the XML layout file is filled to contentview through layoutinflater. The filling process only includes creation attempts, but does not include setting the size. So when will the major and minor attempts be specified? The android development document is explained as follows: the layout is composed of two traversal processes: the measurement process and the layout process. The measurement process is completed by the measure (INT, INT) method, which traverses the view tree from top to bottom. During recursive traversal, each view transmits dimensions and specifications to the lower layer. When the measure method traversal ends, each view stores its own size information. The second process is completed by the layout (INT, Int, Int, INT) method, which also traverses the view tree from top to bottom. During the traversal process, each parent view locates the positions of all child views based on the results of the measurement process. 4. conclusion: The view can obtain its own height and width only after the entire layout is drawn. This process occurs after the oncreate () method. Therefore, getheight () is called before this () and getwidth ()
The result returned by the method is. Solution: Get the result in the UI thread, which involves a method post () code:
1 public class mainactivity extends activity {2 Private button BTN; 3 @ override 4 protected void oncreate (bundle savedinstancestate) {5 super. oncreate (savedinstancestate); 6 setcontentview (R. layout. activity_main); 7 BTN = (button) findviewbyid (R. id. BTN); 8 system. out. println ("BTN of the button. getwidth () is --> "+ Btn. getwidth () 9 + "BTN. getheight () is --> "+ Btn. getheight () 10 + "BTN. getmeasuredwidth () "+ Btn. getmeasuredwidth () 11 + "" + Btn. getmeasuredheight (); 12 BTN. post (New runnable () {13 @ override14 public void run () {15 system. out. println ("BTN of the button. getwidth () is --> "+ Btn. getwidth () 16 + "BTN. getheight () is --> "+ Btn. getheight () 17 + "BTN. getmeasuredwidth () "+ Btn. getmeasuredwidth () 18 + "" + Btn. getmeasuredheight (); 19} 20}); 21} 22 23}
Print the result: we can see that the value of the view is obtained after the sub-post method.

In fact, the POST method is the method used by the sub-thread to access the UI thread. This also provides other methods to access the main thread, if you want to change the UI thread interface in the sub-thread, you can use the following method related knowledge reference address: http://stackoverflow.com/questions/13840007/what-exactly-does-the-post-method-do
  • Activity. runonuithread (runnable)
  • View. Post (runnable)
  • View. postdelayed (runnable, long)
  • Handler

Hackthirteen obtains the width and height of the view in the oncreate () method.

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.