Obtain the width and height of a view in the oncreate method.

Source: Internet
Author: User
Obtain the width and height of a view in the oncreate method.
    Blog type:

  • Android

Sometimes you need to know the width and height of a view component in the oncreate method, and directly call the getwidth (), getheight (), getmeasuredwidth (), getmeasuredheight () gettop (), getleft (), and other methods cannot obtain the actual value, and only 0 is obtained. This is because the view component layout must be completed after the onresume callback. The implementation method is provided below. The ongloballayout callback will be automatically called when the layout is complete.

 

Java code
  1. Final linearlayout imageviewcontainer = (linearlayout) findviewbyid (R. Id. crop_photo_image_view_container );
  2. Imageviewcontainer. getviewtreeobserver (). addongloballayoutlistener (New ongloballayoutlistener (){
  3. Boolean isfirst = true; // It is called twice by default. Only One callback is executed here.
  4. @ Override
  5. Public void ongloballayout (){
  6. If (isfirst ){
  7. Isfirst = false;
  8. // Now the layout is complete. You can obtain the width, height, left, right, and other information of any view component.
  9. Log. I ("CDH", "Global W:" + imageviewcontainer. getmeasuredwidth () + "H:" + imageviewcontainer. getmeasuredheight ());
  10. }
  11. }
  12. });

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.