zoukankan      html  css  js  c++  java
  • Android状态栏高度、虚拟操作栏高度

    public class ScreenUtils {
    
        private ScreenUtils() {
            throw new AssertionError();
        }
    
        /**
         * dp单位转成px
         *
         * @param context context
         * @param dp      dp值
         * @return px值
         */
        public static int dp2px(Context context, int dp) {
            return (int) (dp * context.getResources().getDisplayMetrics().density);
        }
    
        /**
         * 获取屏幕宽度
         */
        public static int getScreenWidth(Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            return dm.widthPixels;
        }
    
        /**
         * 获取屏幕高度
         */
        public static int getScreenHeight(Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            return dm.heightPixels;
        }
    
        /**
         * 获取状态栏高度
         */
        public static int getStatusBarHeight(Context context) {
            // 一般是25dp
            int height = dp2px(context, 20);
            LogUtil.i("common statusBar height:" + height);
            //获取status_bar_height资源的ID
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                height = context.getResources().getDimensionPixelSize(resourceId);
                LogUtil.i("real statusBar height:" + height);
            }
            LogUtil.i("finally statusBar height:" + height);
            return height;
        }
    
        /**
         * 虚拟操作拦(home等)是否显示
         */
        public static boolean isNavigationBarShow(Activity activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Display display = activity.getWindowManager().getDefaultDisplay();
                Point size = new Point();
                Point realSize = new Point();
                display.getSize(size);
                display.getRealSize(realSize);
                return realSize.y != size.y;
            } else {
                boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
                boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
                if (menu || back) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    
        /**
         * 获取虚拟操作拦(home等)高度
         */
        public static int getNavigationBarHeight(Activity activity) {
            if (!isNavigationBarShow(activity))
                return 0;
            int height = 0;
            Resources resources = activity.getResources();
            //获取NavigationBar的高度
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0)
                height = resources.getDimensionPixelSize(resourceId);
            LogUtil.i("NavigationBar的高度:" + height);
            return height;
        }
    }
  • 相关阅读:
    ElementUI 组件不支持@keyup 的解决办法
    ElementUI 实现头部组件和左侧组件效果
    ElementUI 整体页面布局
    vue路由登录拦截
    vue中使用localStorage存储信息
    ElementUI Checkbox 多选框
    vue拦截器qs
    (未完)经典Web漏洞实战演练靶场笔记
    文件包含漏洞实战靶场笔记
    文件解析漏洞总结
  • 原文地址:https://www.cnblogs.com/zuiniub/p/15500235.html
Copyright © 2011-2022 走看看