zoukankan      html  css  js  c++  java
  • 获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度

    1.获取Android屏幕尺寸

    我们可以通过getSize()方法获得屏幕的尺寸

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    • 1
    • 2
    • 3
    • 4
    • 5

    如果不是在Activity里面,则无法使用getWindowManager(),此时可以使用WINDOW_SERVICE获得一个默认的Display

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    • 1
    • 2

    getSize()方法是在API 13 之后才加入的,在API 13之前我们需要这样做

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // 已经过时
    int height = display.getHeight();  // 已经过时
    • 1
    • 2
    • 3

    为了适配所有的设备,我们应该这样写

     if (android.os.Build.VERSION.SDK_INT >= 13) {
                display = getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                width = size.x;
                height = size.y;
            }else {
                display = getWindowManager().getDefaultDisplay();
                width = display.getWidth();
                height = display.getHeight();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    还有另外一种方法,这种方法也可以正确得到屏幕尺寸

      DisplayMetrics metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);
      width = metrics.widthPixels;
      height = metrics.heightPixels;
    • 1
    • 2
    • 3
    • 4

    更简便的写法

     width = getResources().getDisplayMetrics().heightPixels;
     height = getResources().getDisplayMetrics().widthPixels;
    • 1
    • 2

    以上获得的屏幕高度都是包括了状态栏和导航栏的高度的 
    这里写图片描述

    2.获取控件尺寸

    如果我们在onCreate()方法里直接调用getWidth()、getMeasuredWidth()获得的尺寸为0,这是由于在onCreate()中,我们的控件还没有画好,等onCreate()执行完了,我们的控件才被测量出来,我们可以注册一个监听器,用来监听测量结果

    ViewTreeObserver vto  = mButton.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                //移除上一次监听,避免重复监听
                    mButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    //在这里调用getHeight()获得控件的高度
                    buttonHeight = mButton.getHeight();
                }
            });
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.获得状态栏/通知栏的高度

     public static int getStatusBarHeight(Context context){
            Class<?> c = null;
            Object obj = null;
            Field field = null;
            int x = 0, statusBarHeight = 0;
            try {
                c = Class.forName("com.android.internal.R$dimen");
                obj = c.newInstance();
                field = c.getField("status_bar_height");
                x = Integer.parseInt(field.get(obj).toString());
                statusBarHeight = context.getResources().getDimensionPixelSize(x);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            return statusBarHeight;
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.获得导航栏高度

     public int getNavigationBarHeight(Activity activity) {
            Resources resources = activity.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
            //获取NavigationBar的高度
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.去除导航栏

    在onCraete()方法中的setContentView();的之前调用下面这句代码

     requestWindowFeature(Window.FEATURE_NO_TITLE);
    • 1

    6.去除状态栏/通知栏

    在onCraete()方法中的setContentView();的之前调用下面这句代码

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
    ,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  • 相关阅读:
    Google-Hack
    DnsLog盲注
    utf-8编码转换问题
    sql注入 无列名注入
    Python正则
    变形--缩放 scale()
    变形--扭曲 skew()
    变形--旋转 rotate()
    Css3中的变形与动画
    关于伪类元素:before和:after
  • 原文地址:https://www.cnblogs.com/jidan/p/5109114.html
Copyright © 2011-2022 走看看