It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get around this by listening for when the view is actually drawn:

final TextView tv =(TextView)findViewById(R.id.venueLabel);
final ViewTreeObserver observer = tv.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
  @Override
  publicvoid onGlobalLayout()
{ tv.getHeight(); observer.removeGlobalOnLayoutListener(this);
  
}
});

The call to remove the listener is there to prevent repeated invocations of your custom handler on layout changes... if you want to get those, you can omit it.

 

http://stackoverflow.com/questions/8170915/getheight-returns-0-for-all-android-ui-objects

相关文章: