/**
* @author wilber
* @target 4.1
* @requirements:4.1平板隐藏系统栏
* @theme android4.1 平板隐藏System Bar
* @remark 转载请注明出处http://my.oschina.net/wilber
*/
有时开发视频或游戏程序,即使设置了getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE),一不小心触摸了底部区域的小黑点,看视频注意力会分散,游戏中断或注意力分散,颇感不爽。
网上搜罗了相应的资料,好像4.1有个SYSTEM_UI_FLAG_HIDE_NAVIGATION可以处理System Bar的隐藏,不管我在代码里面如何设置,就是不见效果,怀疑是手机上面的功能块吧,没得4.1的实体手机,就不具体深究。网上有些工具软件对其处理,有的需要Root权限,有的只是移动SystemUI软件包位置作消显,感觉不实用,查看View.java的源码,发现还有个标志位SYSTEM_UI_FLAG_SHOW_FULLSCREEN,实践了一会,预期的效果就出来了。
关键代码:
01 | /** |
02 | * 设置系统栏可见性 |
03 | */ |
04 | public static void setSystemBarVisible( final Activity context, boolean visible) { |
05 | int flag = context.getWindow().getDecorView().getSystemUiVisibility(); // 获取当前SystemUI显示状态 |
06 | // int fullScreen = View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN; |
07 | int fullScreen = 0x8 ; // 4.1 View.java的源码里面隐藏的常量SYSTEM_UI_FLAG_SHOW_FULLSCREEN,其实Eclipse里面也可以调用系统隐藏接口,重新提取下android.jar,这里就不述了。 |
08 | if (visible) { // 显示系统栏 |
09 | if ((flag & fullScreen) != 0 ) { // flag标志位中已经拥有全屏标志SYSTEM_UI_FLAG_SHOW_FULLSCREEN |
10 | context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); // 显示系统栏 |
11 | } |
12 | } else { // 隐藏系统栏 |
13 | if ((flag & fullScreen) == 0 ) { // flag标志位中不存在全屏标志SYSTEM_UI_FLAG_SHOW_FULLSCREEN |
14 | context.getWindow().getDecorView().setSystemUiVisibility(flag | fullScreen); // 把全屏标志位加进去 |
15 | } |
16 | } |
17 | } |
这里多加了判断的方法,供调用
1 | /** |
2 | * 判断状态栏是否显示 |
3 | */ |
4 | public static boolean isSystemBarVisible( final Activity context) { |
5 | int flag = context.getWindow().getDecorView().getSystemUiVisibility(); |
6 | // return (flag & View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN) != 0; |
7 | return (flag & 0x8 ) == 0 ; |
8 | } |