Android-Get View width high

Source: Internet
Author: User

In activity you can call View.getwidth, View.getheight (), View.getmeasuredwidth (), View.getgetmeasuredheight () To get the width or height of a view, but returns 0 in the OnCreate (), Onstrart (), Onresume () method, which The interface that should be represented for the current activity does not appear to be added to the Windowphone Decorview or the view to get is not added to the Decorview or the view's visibility property is gone or the width or height of the view is really 0 , so only if the above conditions are not valid to get the width and height of the non-0.

In the view's event callback.

At this time the view has been shown to be added to the Decorview such as Click event Touch Event Focus Event

View view=Findviewbyid (r.id.tv);      View.setonclicklistener (new  Onclicklistener () {                    @Override          publicvoid  OnClick (View v) {              int width = v.getwidth ();          }      });
Onwindowfocuschanged () callback

Get width and height as onwindowfocuschanged () callback method when activity is displayed when added to Decorview

@Override Public voidOnwindowfocuschanged (BooleanHasfocus) {View Iv1=Findviewbyid (R.ID.IV1); View Iv2=Findviewbyid (R.ID.IV2); String MSG1= "Iv1 ' width:" +iv1.getwidth () + "Height:" +iv1.getheight () + "Measuredwidth:" +iv1.getmeasuredwidth () + "Measuredheight :"+iv1.getmeasuredheight (); String MSG2= "Iv2 ' width:" +iv2.getwidth () + "Height:" +iv2.getheight () + "Measuredwidth:" +iv2.getmeasuredwidth () + "Measuredheight :"+iv2.getmeasuredheight (); LOG.I ("Onwindowfocuschanged ()" +MSG1); LOG.I ("Onwindowfocuschanged ()" +MSG2); Super. onwindowfocuschanged (Hasfocus); }
Delay

After 300 milliseconds after the Onresume method, the interface is displayed after 300 milliseconds after the completion of the Onresume execution.

View.postdelayed (NewRunnable () {@Override Public voidrun () {View iv1=Findviewbyid (R.ID.IV1); View Iv2=Findviewbyid (R.ID.IV2); String MSG1= "Iv1 ' width:" +iv1.getwidth () + "Height:" +iv1.getheight () + "Measuredwidth:" +iv1.getmeasuredwidth () + "Measuredheight :"+iv1.getmeasuredheight (); String MSG2= "Iv2 ' width:" +iv2.getwidth () + "Height:" +iv2.getheight () + "Measuredwidth:" +iv2.getmeasuredwidth () + "Measuredheight :"+iv2.getmeasuredheight (); LOG.I ("Onwindowfocuschanged ()" +MSG1); LOG.I ("Onwindowfocuschanged ()" +MSG2); }          }, 300);
In methods such as OnCreate () or Onresume (), you need to

Getviewtreeobserver (). Addongloballayoutlistener () to add a view add callback in the callback to get the width or height after getting the view to delete the callback

View.getviewtreeobserver (). Addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () {@Override Public voidongloballayout () {view.postdelayed (NewRunnable () {@Override Public voidrun () {View iv1=Findviewbyid (R.ID.IV1); View Iv2=Findviewbyid (R.ID.IV2); String MSG1= "Iv1 ' width:" +iv1.getwidth () + "Height:" +iv1.getheight () + "Measuredwidth:" +iv1.getmeasuredwidth () + "Measuredheight :"+iv1.getmeasuredheight (); String MSG2= "Iv2 ' width:" +iv2.getwidth () + "Height:" +iv2.getheight () + "Measuredwidth:" +iv2.getmeasuredwidth () + "Measuredheight :"+iv2.getmeasuredheight (); LOG.I ("Onwindowfocuschanged ()" +MSG1); LOG.I ("Onwindowfocuschanged ()" +MSG2); }              }, 300); }      });
I'm the dividing line of the king of the Land Tiger.

Reference: http://blog.csdn.net/nailsoul/article/details/25909313

Android-Get View width high

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.