您的位置:首页 > 移动开发 > Android开发

Android 检查设备是否存在 导航栏 NavigationBar

2015-07-07 10:31 435 查看
/**
* 检查设备是否有导航栏
* @param activity
* @return
*/
@SuppressLint("NewApi")
public static boolean checkDeviceHasNavigationBar(Context activity) {

//通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
boolean hasMenuKey = false;
boolean hasBackKey = false;
try {
hasMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();
} catch (java.lang.NoSuchMethodError e) {
// TODO: handle exception
}
try {
hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
} catch (java.lang.NoSuchMethodError e) {
// TODO: handle exception
}

if (!hasMenuKey && !hasBackKey) {
// 做任何你需要做的,这个设备有一个导航栏
return true;
}
return false;
}

/**
* 获取导航栏高度 ,此方法不会检查导航栏是否存在,直接返回数值。所以可能手机没有显示导航栏,但是高度依然返回
* @param activity
* @return
*/
public static int getNavigationBarHeight(Context activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
//获取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
return height;
}

/**
* 获取导航栏高度,此方法会根据手机是否存在导航栏,返回相应的数值
* @param activity
* @return
*/
public static int getNavigationBarHeightEx(Context activity){
if(checkDeviceHasNavigationBar(activity)){
return getNavigationBarHeight(activity);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: