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