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(); }
});
评论(0)