Android获取应用大小、状态栏高度

在Android中,有时我们要获取屏幕的大小,有时要获取状态栏的高度,下面就是一下获取的方法。

    /**
     * 得到屏幕大小
     */
    protected void getAreaScreen() {
        Display display = getWindowManager().getDefaultDisplay();
        Point outP = new Point();
        display.getSize(outP);
        Log.d(TAG, "AreaScreen:" + " width=" + outP.x + " height=" + outP.y);
    }
    /**
     * 得到应用的大小
     */
    protected void getAreaApplication() {
        Rect outRect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        statusHeight = outRect.top;
        applicationHeight = outRect.height();
        Log.d(TAG, "AreaApplication:" + " width=" + outRect.width()
                + " height=" + outRect.height() + " top=" + outRect.top
                + " bottom=" + outRect.bottom);
    }
    /**
     * 得到View绘制的大小
     */
    protected void getAreaView() {
        // 用户绘制区域
        Rect outRect = new Rect();
        getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
        viewHeight = outRect.height();
        Log.d(TAG, "AreaView:" + " width=" + outRect.width() + " height="
                + outRect.height());
    }
    /**
     * 设置View的Padding
     */
    protected void setPadding(View v, int left, int top, int right, int bottom) {
        v.setPadding(left, top, right, bottom);
        v.requestLayout();
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容