The simplest and hardest--how to get to the height of the Android control

Source: Internet
Author: User

Problem

How to get a control of the length and height, I believe many friends at the first sight of this problem will feel very simple, directly in the OnCreate inside call getwidth, getmeasuredwidth can not be obtained, but, in fact, there is no simple, not letter words, You can try it, in OnCreate, you can't get a long-width value, always 0.

Reason

This is why, in fact, familiar with the view drawing process of friends should be seen at a glance, in OnCreate, our controls are not actually painted well, in other words, the OnCreate method is finished, we define the control will be measured (measure), So we get the height or width of the control in the OnCreate method through View.getheight () is definitely 0.

Solve

NO1:

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

This method is simple, we measure it ourselves.


No2:

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

This method, we need to register a Viewtreeobserver listener callback, this listener callback, is dedicated to listen to the drawing, since it is the monitoring of the drawing, then we can naturally obtain the measured value, at the same time, we listen to each monitor before the remove, avoid repeated monitoring.


No3:

Viewtreeobserver vto = Imageview.getviewtreeobserver ();   Vto.addongloballayoutlistener (New Ongloballayoutlistener () {     @Override public       void Ongloballayout () {         Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);         Imageview.getheight ();        Imageview.getwidth ();    }   });   

This method is basically the same as the 2nd method, but he is the global layout change listener, so it is most recommended to use.


OK, now it seems that the simple question is not so simple.

Above.

The simplest and hardest--how to get to the height of the Android control

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.