美文网首页
Android获取应用大小、状态栏高度

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

作者: 匡风含情 | 来源:发表于2017-03-12 14:39 被阅读1325次

在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();
    }

相关文章

网友评论

      本文标题:Android获取应用大小、状态栏高度

      本文链接:https://www.haomeiwen.com/subject/fnwtnttx.html