获取NavigationBar状态与高度

踩到一个坑 华为全面屏手机获取NavigationBar高度,明明没有导航栏缺返回高度120,为避免这个问题先判断NavigationBar是否显示再去获取高度。

//NavigationBar状态是否是显示
public boolean isNavigationBarShow(Activity mActivity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y != size.y;
    } else {
        boolean menu = ViewConfiguration.get(mActivity).hasPermanentMenuKey();
        boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        if (menu || back) {
            return false;
        } else {
            return true;
        }
    }
}


/**
 * 获取NavigationBar高度
 *
 */
public static int getNavigationBarHeight(Context context) {
    return getSizeByReflection(context, "navigation_bar_height");
}

public static int getSizeByReflection(Context context, String field) {
    int size = -1;
    try {
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField(field).get(object).toString());
        size = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}

 

你可能感兴趣的:(填坑之路)