Savage F. Morgan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

posted on 2014-04-11 17:21  罗斯摩根  阅读(169)  评论(0编辑  收藏  举报