首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式获取Android导航栏的高度和宽度?

如何以编程方式获取Android导航栏的高度和宽度?
EN

Stack Overflow用户
提问于 2013-11-28 18:59:53
回答 18查看 146.4K关注 0票数 141

屏幕底部的黑色导航栏在Android中不容易移除。它从3.0开始就是Android的一部分,作为硬件按钮的替代品。这是一张图片:

如何以像素为单位获取此UI元素的宽度和高度的大小?

EN

回答 18

Stack Overflow用户

回答已采纳

发布于 2013-11-28 19:03:18

试试下面的代码:

代码语言:javascript
复制
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;
票数 197
EN

Stack Overflow用户

发布于 2015-04-13 23:50:39

我通过比较应用程序可用屏幕大小和实际屏幕大小来获得导航栏大小。我假设当应用程序可用屏幕尺寸小于实际屏幕尺寸时会出现导航栏。然后我计算导航栏的大小。此方法适用于API 14及更高版本。

代码语言:javascript
复制
public static Point getNavigationBarSize(Context context) {
    Point appUsableSize = getAppUsableScreenSize(context);
    Point realScreenSize = getRealScreenSize(context);

    // navigation bar on the side
    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;
}

更新有关考虑显示剪切的解决方案,请查看约翰的回答。

票数 105
EN

Stack Overflow用户

发布于 2015-04-29 16:22:27

对于某些设备,NavigationBar的高度会有所不同,但对于某些方向,也会有所不同。首先,你必须检查设备是否有导航栏,然后是平板电脑还是非平板电脑(手机),最后,你必须查看设备的方向,以获得正确的高度。

代码语言:javascript
复制
public int getNavBarHeight(Context c) {
         int result = 0;
         boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
         boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

         if(!hasMenuKey && !hasBackKey) {
             //The device has a navigation bar
             Resources resources = c.getResources();

             int orientation = resources.getConfiguration().orientation;
             int resourceId;
             if (isTablet(c)){
                 resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");
             }  else {
                 resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_width", "dimen", "android");     
             }

             if (resourceId > 0) {
                 return resources.getDimensionPixelSize(resourceId);
             }
         }
         return result;
} 


private boolean isTablet(Context c) {
    return (c.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
票数 42
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20264268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档