How to get to the height of the Android control

Source: Internet
Author: User


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.


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.



[Java]View Plaincopyprint?
  1. int w = View.MeasureSpec.makeMeasureSpec (0,
  2. View.MeasureSpec.UNSPECIFIED);
  3. int h = View.MeasureSpec.makeMeasureSpec (0,
  4. View.MeasureSpec.UNSPECIFIED);
  5. Imageview.measure (W, h);
  6. int height = imageview.getmeasuredheight ();
  7. int width = imageview.getmeasuredwidth ();

This method is simple, we measure it ourselves.


[Java]View Plaincopyprint?
  1. Viewtreeobserver vto = Imageview.getviewtreeobserver ();
  2. Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
  3. Public Boolean Onpredraw () {
  4. Vto.removeonpredrawlistener (this);
  5. int height = imageview.getmeasuredheight ();
  6. int width = imageview.getmeasuredwidth ();
  7. return true;
  8. }
  9. });

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.


[Java]View Plaincopyprint?
  1. Viewtreeobserver vto = Imageview.getviewtreeobserver ();
  2. Vto.addongloballayoutlistener (new Ongloballayoutlistener () {
  3. @Override
  4. public void Ongloballayout () {
  5. Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
  6. Imageview.getheight ();
  7. Imageview.getwidth ();
  8. }
  9. });

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.


How to get to the height of the Android control

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: 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.