4 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個贊
試試下面的代碼:
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個贊
NavigationBar的高度對于某些設(shè)備有所不同,但對于某些方向也有所不同。首先,你必須檢查設(shè)備是否有一個導(dǎo)航欄,那么如果該設(shè)備是平板電腦或不片劑(手機(jī)),最后你得看設(shè)備的方向,以獲得正確的高度。
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;
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個贊
實(shí)際上,平板電腦(至少是Nexus 7)上的導(dǎo)航欄在縱向和橫向上都有不同的尺寸,因此此功能應(yīng)如下所示:
private int getNavigationBarHeight(Context context, int orientation) {
Resources resources = context.getResources();
int id = resources.getIdentifier(
orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape",
"dimen", "android");
if (id > 0) {
return resources.getDimensionPixelSize(id);
}
return 0;
}
- 4 回答
- 0 關(guān)注
- 1008 瀏覽
添加回答
舉報