Android NavigationBar的显示状态与高度获取

2018-12-20  本文已影响181人  Summer舒舒

前言

最近在处理PopupWindow的显示时,遇到了全面屏的适配问题,展示NavigationBar时mContentView底部被遮挡,隐藏NavigationBar时mContentView不能充满。
其中关键在于NavigationBar显示状态和高度的获取,这样mContentView的高度就能随之确定,就不会产生被遮挡或不能充满的UI问题。

NavigationBar相关方法

试验了若干方法,最后总结如下:

1、判断是否支持NavigationBar
public static boolean hasNavigationBar(Context var0) {
        boolean var1 = false;
        int var2;
        Resources var4;
        if((var2 = (var4 = var0.getResources()).getIdentifier("config_showNavigationBar", "bool", "android")) > 0) {
            var1 = var4.getBoolean(var2);
        }

        try {
            Class var5;
            String var6 = (String)(var5 = Class.forName("android.os.SystemProperties")).getMethod("get", new Class[]{String.class}).invoke(var5, new Object[]{"qemu.hw.mainkeys"});
            if("1".equals(var6)) {
                var1 = false;
            } else if("0".equals(var6)) {
                var1 = true;
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return var1;
    }

注意:

2、判断是否显示NavigationBar
public static boolean isNavigationBarShowing(Context var0) {
        if(!hasNavigationBar(var0)) {
            return false;
        } else {
            if(VERSION.SDK_INT >= 17) {
                String var1;
                if(SysUtilsNew.isXiaomi()) {
                    var1 = "force_fsg_nav_bar";
                    if(Global.getInt(var0.getContentResolver(), var1, 0) != 0) {
                        return false;
                    }

                    return true;
                }

                if(SysUtilsNew.isVivo()) {
                    var1 = "navigation_gesture_on";
                    if(Secure.getInt(var0.getContentResolver(), var1, 0) != 0) {
                        return true;
                    }

                    return false;
                }
            }

            return true;
        }
    }

注意:

3、获取NavigationBar的高度
 public static int getNavigationBarHeight(Context var0) {
        boolean var1 = ViewConfiguration.get(var0).hasPermanentMenuKey();
        int var2;
        return (var2 = var0.getResources().getIdentifier("navigation_bar_height", "dimen", "android")) > 0 && !var1?var0.getResources().getDimensionPixelSize(var2):0;
    }

注意:

解决办法

mContentView是自定义View,我重写了自定义View的onMeasure方法,动态计算和设置其高度,运行ok。兼容了NavigationBar常驻、不显示NavigationBar、支持控制NavigationBar显示状态的米6X、米8全面屏手机这三类机型。

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (HEIGHT <= 0) {
            HEIGHT = ScreenUtils.getRealScreenHeight(getContext()) - ScreenUtils.getStatusBarHeight();

            if (ScreenUtils.isNavigationBarShowing(getContext())) {
                HEIGHT -= ScreenUtils.getNavigationBarHeight2(getContext());
            }
        }

        int heightSpec = View.MeasureSpec.makeMeasureSpec(HEIGHT, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightSpec);
        setMeasuredDimension(widthMeasureSpec, heightSpec);
    }

补充

1、获取屏幕全部的高度,RealHeight=StatusBarHeight+ContentHeight+NavigationBarHeight
 public static int getRealScreenHeight(Context var0) {
        int var1 = 0;
        Display var5 = ((WindowManager)var0.getSystemService("window")).getDefaultDisplay();
        DisplayMetrics var2 = new DisplayMetrics();

        try {
            ReflectionUtils.getClassForName("android.view.Display").getMethod("getRealMetrics", new Class[]{DisplayMetrics.class}).invoke(var5, new Object[]{var2});
            var1 = var2.heightPixels;
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return var1;
    }
2、获取状态栏的高度
public static int getStatusBarHeight() {
        Resources var0;
        int var1;
        if(STATUS_BAR_HEIGHT == -1 && (var1 = (var0 = getResource()).getIdentifier("status_bar_height", "dimen", "android")) > 0) {
            STATUS_BAR_HEIGHT = var0.getDimensionPixelSize(var1);
        }

        return STATUS_BAR_HEIGHT;
}
上一篇下一篇

猜你喜欢

热点阅读