Screen adaptation of mobile phone screen adaptation with virtual buttons

Recently, I encountered a problem of screen adaptation with virtual buttons during project development. For example, Huawei P9, Xiaomi MAX and other models, the layout of the entire screen is up, I think it should be a virtual button problem. So, after some tossing, I found the following solution:

  • Get the actual display size and height of the screen
//获取屏幕尺寸,不包括虚拟功能高度
getWindowManager().getDefaultDisplay().getHeight();


  •  Get the original height of the screen
 private int getHeight() {
        int dpi = 0;
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            dpi = dm.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dpi;
    }

After two height comparisons, the height of the virtual key can be obtained. The resolution of the screen with virtual keyboard is 1920*1080, but it actually recognizes the real height ((1920-virtual keyboard height)*1080).
solve:
You can write a set of dimens files of a specific size. For example, the height of the virtual button on Huawei p9 is 120px. Then you can add a set of values-1800X1080 and copy 1920X1080 to  values-1800X1080. Perfect solution!

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/72722837