Android-获取窗口的宽度与高度

时间:2014-06-26 23:32:08   收藏:0   阅读:217

在开发的过程中,有时候需要获取窗口的大小,在OnCreate中由于窗口还未绘制因此通过GetWidth()与GetHeight()得到的宽高都为0,那应该如何获取窗口的大小呢,上代码:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @SuppressWarnings("deprecation")
  @Override 
  public void onGlobalLayout() {            
    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);     int width = layout.getMeasuredWidth(); int height = layout.getMeasuredHeight(); }
});

 

Android-获取窗口的宽度与高度,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!