Android OnCreate () Get control height and width in two ways _android

Source: Internet
Author: User

Get control height and width in Android OnCreate ()

Try to get control height and width in oncreate () Children's shoes are aware that the getwidth () and GetHeight () method return is 0, specific reasons to see the activity life cycle will understand.

Code on:

Method One:

 
    int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
    int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
    Ssidtext.measure (W, h);
    int width =ssidtext.getmeasuredwidth ();
    int height =ssidtext.getmeasuredheight ();

Method Two:

    Viewtreeobserver vto = Ssidtext.getviewtreeobserver ();
    Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public
      Boolean Onpredraw () {
        int height = Ssidtext.getmeasuredheight ();
        int width = ssidtext.getmeasuredwidth ();
        return true;
      }
    });

Specifically which one can be based on the needs of their own choice.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.