1. 程式人生 > >獲取StatusBar,NavigationBar高度及判斷是否存在

獲取StatusBar,NavigationBar高度及判斷是否存在

判斷是否存在NavigationBar:

private static boolean checkDeviceHasNavigationBar(Context context) {

    boolean hasNavigationBar = false;
    Resources rs = context.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }


    return hasNavigationBar;
}

獲取NavigationBar的高度:

private static int getNavigationBarHeight(Context context) {
    int navigationBarHeight = 0;
    Resources rs = context.getResources();
    int id = rs.getIdentifier("navigation_bar_height", "dimen", "android");
    if (id > 0 && checkDeviceHasNavigationBar(context)) {
        navigationBarHeight = rs.getDimensionPixelSize(id);
    }
    return navigationBarHeight;
}

獲取StatusBar的高度:

public static int getStatusBarHeight(Context context)
{
int result = 0;
int resourceId = context.getResources().getIdentifier(“status_bar_height”, “dimen”, “android”);
if (resourceId > 0)
{
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}