Android学习笔记之屏幕宽高、状态栏宽高、标题宽高



首先要了解的是安卓的屏幕分布区域(这里我采用的是1080 * 1920的屏幕),如下图所示:

这里写图片描述

通过上图我们就可以知道手机屏幕主要的三个区域了,下面我们开始使用代码来获取这三个区域的宽高度。

这里需要注意一下,这里我们所测的高度并不是1920,因为最底下有一个模拟的虚拟按钮占据了一定的空间高度,所以具体的高度,会根据手机的具体情况获取。

为什么我有时候在使用getLeft(), getRight(), getTop(), getBottom()它们得到的结果是0?
出现这种情况可能是在刚启动程序,程序刚开始绘制 view 的时候,你马上使用代码去捕获上面的值。这个时候,由于view 是刚开始绘制的,你得到的就会是 0,所以,这里我们所有的测试都放在点击事件里面来进行。

  1. 获取整个屏幕的宽度:

可以通过一下5中形式获取整个屏幕的宽高度

/**
     * 获取屏幕的总宽度
     * 采用5中方法
     */
    public void getTotalScreenWidthAndHeight() {
        //方法1
        WindowManager manager1 = getWindowManager();
        Display display = manager1.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        Log.d(TAG, "getTotalScreenWidth: width = " + point.x);
        Log.d(TAG, "getTotalScreenWidth: height = " + point.y);
        //方法2
        WindowManager manager2 = (WindowManager) getSystemService(WINDOW_SERVICE);
        int width = manager2.getDefaultDisplay().getWidth();
        int height = manager2.getDefaultDisplay().getHeight();
        Log.d(TAG, "getTotalScreenWidth: width = " + width);
        Log.d(TAG, "getTotalScreenWidth: height = " + height);
        //方法3
        WindowManager manager3 = getWindowManager();
        int width1 = manager3.getDefaultDisplay().getWidth();
        int height1 = manager3.getDefaultDisplay().getHeight();
        Log.d(TAG, "getTotalScreenWidth: width = " + width1);
        Log.d(TAG, "getTotalScreenWidth: height = " + height1);
        //方法4
        WindowManager manager4 = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager4.getDefaultDisplay().getMetrics(outMetrics);
        int width2 = outMetrics.widthPixels;
        int height2 = outMetrics.heightPixels;
        Log.d(TAG, "getTotalScreenWidth: width = " + width2);
        Log.d(TAG, "getTotalScreenWidth: height = " + height2);
        //方法5
        Resources resources  = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density1 = dm.density;
        int width3 = dm.widthPixels;
        int height3 = dm.heightPixels;
        Log.d(TAG, "getTotalScreenWidth: width = " + width3);
        Log.d(TAG, "getTotalScreenWidth: height = " + height3);
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

获取的结果为:width = 1080 height = 1794 上面也已经说过了,这里为什么不是1920,因为虚拟按钮部分占据了126的高度

  1. 获取标题栏的高度以及到底部的高度
/**
     * 获取屏幕标题栏到底部的高度
     */
    public void getScreenTitleHeight() {
        Rect outRect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        //打印标题栏的高度
        Log.d(TAG, "height = " + outRect.top);
        //打印标题栏到底部的高度
        Log.d(TAG, "width = " + outRect.width() + " height = " + outRect.height());
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果为:height = 63 outRect.top是状态栏占用的高度为63 width = 1080 height = 1731 宽度不变是毫无疑问的,高度是1731 刚好整个屏幕的高度(1794) - (1731)标题栏到底部的高度 就刚好是63了。

  1. 获取view绘制区域的高度:
/**
     * 获取view绘制区域的高度
     */
    public void getScreenViewHeight(){
        Rect outRect = new Rect();
        getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
        Log.d(TAG, "height = " + outRect.height());
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

结果为:height = 1584 表示的是view绘制区域到底部的高度, 这样获取的结果就出来了,下面总结一下整个的需要掌握的高度

屏幕总宽高度: 1080 * 1794

屏幕标题栏到底部的宽高度 : 1080 * 1731

屏幕状态栏的高度:63

屏幕view绘制区域的宽高度 : 1080 * 1584

屏幕的标题栏高度为:1794(整个屏幕高度) - 1584(view绘制区的高度) - 63(状态栏的高度) = 147(标题栏的高度)。

另外补充:


@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Rect rect = new Rect(); Window win = getWindow(); win.getDecorView().getWindowVisibleDisplayFrame(rect); //状态栏高度
        int statusBarHeight = rect.top; //状态栏与标题栏高度总和
        int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentViewTop - statusBarHeight; Log.d("状态栏高度:" + statusBarHeight); Log.d("标题栏高度:" + titleBarHeight); }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值