Check if Android device has software or hardware navigation buttons

后端 未结 2 1697
自闭症患者
自闭症患者 2021-01-04 19:38

I\'m writing an Android app with a transparent navigation bar. To adjust the layout accordingly, I need to know whether the device has software or hardware buttons.

2条回答
  •  醉酒成梦
    2021-01-04 20:16

    The answer posted here did the trick. Just for completeness, here is the code I am using now:

    private static boolean hasImmersive;
    private static boolean cached = false;
    
    @SuppressLint ("NewApi")
    public static boolean hasImmersive(Context ctx) {
    
        if (!cached) {
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                hasImmersive = false;
                cached = true;
                return false;
            }
            Display d = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    
            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);
    
            int realHeight = realDisplayMetrics.heightPixels;
            int realWidth = realDisplayMetrics.widthPixels;
    
            DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);
    
            int displayHeight = displayMetrics.heightPixels;
            int displayWidth = displayMetrics.widthPixels;
    
            hasImmersive = (realWidth > displayWidth) || (realHeight > displayHeight);
            cached = true;
        }
    
        return hasImmersive;
    }
    

提交回复
热议问题