Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (41 sloc) 1.84 KB

是否存在

public static boolean isSoftNavigationBarAvailable(Context context) {
    boolean hasSoftwareKeys;

//    // 这个 在 OPPO R9m (Android 6) 返回 true, 但是 应该返回 false
//    int id = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
//    hasSoftwareKeys = (id > 0 && context.getResources().getBoolean(id));

    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = manager.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;

        hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else {
//        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
        hasSoftwareKeys = !(hasBackKey && hasHomeKey);
    }

    return hasSoftwareKeys;
}

高度

public static int getSoftNavigationBarHeight(Context context) {
    int result = 0;

    int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");

    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }

    return result;
}

参看