android - 如何在 Android 中真正获取导航栏高度

标签 android navigation

我拥有一台 HTC One A9,它可以隐藏导航栏。在我的应用程序中,我需要获取导航栏的高度并设置与之对应的内边距。这是我现在的问题:当我隐藏导航栏时,填充仍在设置中(甚至 Snapchat 也有这个问题)。我的问题是:是否有替代代码可以使其正常工作?

public static int getNavBarHeight(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;
}

感谢您的帮助!

最佳答案

这是我用来获取导航栏大小的代码。它的高度将在 Point.y 中

归功于 this answer

public static Point getNavigationBarSize(Context context) {
    Point appUsableSize = getAppUsableScreenSize(context);
    Point realScreenSize = getRealScreenSize(context);

    // navigation bar on the right
    if (appUsableSize.x < realScreenSize.x) {
        return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
    }

    // navigation bar at the bottom
    if (appUsableSize.y < realScreenSize.y) {
        return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
    }

    // navigation bar is not present
    return new Point();
}

public static Point getAppUsableScreenSize(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

public static Point getRealScreenSize(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealSize(size);
    } else if (Build.VERSION.SDK_INT >= 14) {
        try {
            size.x = (Integer)     Display.class.getMethod("getRawWidth").invoke(display);
            size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
    } catch (IllegalAccessException e) {} catch     (InvocationTargetException e) {} catch (NoSuchMethodException e) {}
    }

    return size;
}

编辑:为了回答您的问题,我不得不使用此功能,因为我想将 ResideMenu 添加到我的应用程序,但由于导航栏的原因,最终在我的应用程序底部出现了一个奇怪的空白页边距。

所以我这样编辑了 ResideMenu 添加的这个功能:

@Override
protected boolean fitSystemWindows(Rect insets) {
    // Applies the content insets to the view's padding, consuming that content (modifying the insets to be 0),
    // and returning true. This behavior is off by default and can be enabled through setFitsSystemWindows(boolean)
    // in API14+ devices.

    int bottomPadding = insets.bottom;

    Point p = getNavigationBarSize(getContext());

    if (Build.VERSION.SDK_INT >= 21 && p.x != 0) {
        Resources resources = getContext().getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            bottomPadding += resources.getDimensionPixelSize(resourceId);
        }
    }

    this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
            viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
    insets.left = insets.top = insets.right = insets.bottom = 0;
    return true;
}

希望对你有所帮助。

关于android - 如何在 Android 中真正获取导航栏高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514167/

相关文章:

javascript - Cordova 3.5.0 上的 SQLite 插件无法正常工作

wpf - WPF 不可编辑组合框中的选项卡导航问题

android - 如何更改android中的状态栏文本颜色?

android - 如何在 Android.mk 中添加 C-only 选项?

java - 从数据库获取并将其显示到 android 表

java - 从 Nutiteq map 中删除标记的正确方法

html - CSS Wrapper 在导航时移动

navigation - React-native 导航实验示例?

html - Flexbox 导航栏、容器和语义