1. 程式人生 > >獲取狀態列和導航欄高度

獲取狀態列和導航欄高度

獲取狀態列高度

public static int getStatusHeight(Context context) {
    int status_bar_height = 0;
    int h = context.getResources().getDimensionPixelSize(
            context.getResources().getIdentifier("status_bar_height", "dimen", "android"));
    if(h > 0){
        status_bar_height = h;
    }

    return status_bar_height;
}

獲取導航欄高度

public static int getNavBarHeight(Context context, boolean islandscape) {
    int navBarHeight = 0;

    if (!ViewConfiguration.get(context).hasPermanentMenuKey()) {
        String name = "navigation_bar_height";
        if (islandscape) {
            name = "navigation_bar_height_landscape";
        }
        navBarHeight = context.getResources().getDimensionPixelSize(
                context.getResources().getIdentifier(name, "dimen", "android"));
    }

    return navBarHeight;
}