• 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;
        }
    }
  • 相关阅读:
    mysql启动失败
    mini.open参数传递
    json的key动态赋值
    Python文件格式 .py .pyc .pyw .pyo .pyd的主要区别
    centos 7.2 安装mongodb 3.4.4免编译
    tomcat 8.0安装ssl证书,及centos7.2 的openssl升级到最新版本,及ERR_SSL_OBSOLETE_CIPHER错误解决
    云服务器 Centos7.0 部署
    笔记
    oracle 视图的创建,游标,left join
    在Extjs中对日期的处理,以及在后端数据在SQL语句的判断处理
  • 原文地址:https://www.cnblogs.com/zuiniub/p/15500235.html
Copyright © 2020-2023  润新知