美文网首页
view位置小坑

view位置小坑

作者: ccsosnfs | 来源:发表于2020-03-12 12:21 被阅读0次

    getLocationOnScreen() will get the location based on the phone screen.

    getLocationInWindow() will get the location based on the activity window. 水滴屏获取位置用这种

    |--phone screen--------------------- |
    |--------status bar---------------------|
    |                                                      |
    |-------------------------------------------|
    |--------activity window------ -------|
    |                                                      |
    |                                                      |
    |                                                      |
    |                                                      |
    |                                                      |
    |                                                      |
    |-------------------------------------------|
    

    2.statusBarHeight会随着有无通知改变

    3.是否有刘海屏 水滴屏

     /**
         * 是否有刘海屏
         *
         * @return
         */
        public static boolean hasNotchInScreen(Activity activity) {
            if (activity == null || activity.isFinishing()) return false;
    
            boolean result = false;
    
            // 通过其他方式判断是否有刘海屏  目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
            String manufacturer = Build.MANUFACTURER;
    
            Log.i("cchen", "manufacturer " + manufacturer);
            if (TextUtils.isEmpty(manufacturer)) {
                return false;
            } else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
                result = hasNotchHw(activity);
            } else if (manufacturer.equalsIgnoreCase("xiaomi")) {
                result = hasNotchXiaoMi(activity);
            } else if (manufacturer.equalsIgnoreCase("oppo")) {
                result = hasNotchOPPO(activity);
            } else if (manufacturer.equalsIgnoreCase("vivo")) {
                result = hasNotchVIVO(activity);
            }
    
            if(result) return true;
    
            // android  P 以上有标准 API 来判断是否有刘海屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
                Log.i("cchen", "displayCutout " + displayCutout);
                if (displayCutout != null) {
                    // 说明有刘海屏
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 判断vivo是否有刘海屏
         * https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
         *
         * @param activity
         * @return
         */
        private static boolean hasNotchVIVO(Activity activity) {
            try {
                Class<?> c = Class.forName("android.util.FtFeature");
                Method get = c.getMethod("isFeatureSupport", int.class);
                return (boolean) (get.invoke(c, 0x20));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 判断oppo是否有刘海屏
         * https://open.oppomobile.com/wiki/doc#id=10159
         *
         * @param activity
         * @return
         */
        private static boolean hasNotchOPPO(Activity activity) {
            return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
        }
    
        /**
         * 判断xiaomi是否有刘海屏
         * https://dev.mi.com/console/doc/detail?pId=1293
         *
         * @param activity
         * @return
         */
        private static boolean hasNotchXiaoMi(Activity activity) {
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("getInt", String.class, int.class);
                return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 判断华为是否有刘海屏
         * https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
         *
         * @param activity
         * @return
         */
        private static boolean hasNotchHw(Activity activity) {
    
            try {
                ClassLoader cl = activity.getClassLoader();
                Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
                Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
                return (boolean) get.invoke(HwNotchSizeUtil);
            } catch (Exception e) {
                return false;
            }
        }
    
    

    4.Button在约束布局中挡住其他view,是因为button自带elevation,在Z轴上高于其他view。
    Button和TextView的不同
    Button 有背景,点击,按下都有相应的效果。
    可点击。
    默认有焦点。
    样式不同,比如颜色,位置等

    5.removeView由于有LayoutTransition而删除失败

    6.弹出dialog时,window不退出全屏,虚拟键不弹出
    在show 之前 ,getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

    相关文章

      网友评论

          本文标题:view位置小坑

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