1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public int getNavigationBarHeight(Context context) { Resources res = context.getResources(); int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (hasNavBar(context)) { String key; if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { key = NAV_BAR_HEIGHT_RES_NAME; } else { if (!isNavigationAtBottom()) return 0; key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME; } return getInternalDimensionSize(res,key); } } return result; }
private int getInternalDimensionSize(Resources res, String key) { int result = 0; int resourceId = res.getIdentifier(key, "dimen", "android"); if (resourceId > 0) { result = res.getDimensionPixelSize(resourceId); } return result; }
private boolean isNavigationAtBottom() { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); float mSmallestWidthDp = getSmallestWidthDp(wm); return (mSmallestWidthDp >= 600 || mInPortrait); }
private float getSmallestWidthDp(WindowManager wm) { DisplayMetrics metrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { wm.getDefaultDisplay().getRealMetrics(metrics); } else { wm.getDefaultDisplay().getMetrics(metrics); } float widthDp = metrics.widthPixels / metrics.density; float heightDp = metrics.heightPixels / metrics.density; return Math.min(widthDp, heightDp); }
|