android开发笔记之虚拟键(navigation bar)的一些状态的读取

最近有一个UI适配的问题,涉及到了虚拟键(navigation bar),通过查找资料,就对navigation bar的一些参数进行读取,希望大家能用的上:

(1)读取虚拟键(navigation bar)一开始是否显示的配置值(config_showNavigationBar):

public static boolean isVirtualKeyShow(Activity context){
		if(isHighThanOrEqual4point0() == true){			
			Resources resources = context.getResources();
			int resourceId = resources.getIdentifier("config_showNavigationBar", "bool", "android");
			if (resourceId > 0) {
			    return resources.getBoolean(resourceId);
			}								
		}
		return false;		
	}

(2)读取虚拟键(navigation bar)的高(navigation_bar_height),宽(navigation_bar_width)和navigation_bar_height_landscape值,通过分析,最后,发现高是对的,但是宽的值对应不上,大家要小心。

	public static int getNavigationBarHeight(Activity context){
		Resources resources = context.getResources();
		int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
		if (resourceId > 0) {
		    return resources.getDimensionPixelSize(resourceId);
		}
		return 0;
	}
	
	public static int getNavigationBarWidth(Activity context){
		Resources resources = context.getResources();
		int resourceId = resources.getIdentifier("navigation_bar_width", "dimen", "android");
		if (resourceId > 0) {
		    return resources.getDimensionPixelSize(resourceId);
		}
		return 0;
	}
	
	public static int getNavigationBarHeithtLandscape(Activity context){
		Resources resources = context.getResources();
		int resourceId = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
		if (resourceId > 0) {
		    return resources.getDimensionPixelSize(resourceId);
		}
		return 0;
	}

最后,还有一个就是帮助方法,也就是判断系统版本是否高于4.0的方法:

	public static boolean isHighThanOrEqual4point0(){
		if(getAPIVersion() >= 14){
			return true;
		}else{
			return false;
		}
	}
	
	public static int getAPIVersion(){
		int APIVersion;
		try {
			APIVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			APIVersion = 0;
		}
		return APIVersion;
	}


你可能感兴趣的:(android,NavigationBar)