首先判斷手機是否有虛拟欄,我這邊隻是做了很簡單的判斷,判斷手機是否有回退和Home鍵
//是否有下方虛拟欄
private static boolean isNavigationBarAvailable() {
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
return (!(hasBackKey && hasHomeKey));
}
擷取下方虛拟欄高度
public static int getNavigationBarHeight() {
if (isNavigationBarAvailable()) {
Resources resources = ApplicationLoader.context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
}
return 0;
}