1、网络上关于隐藏StatusBar的方案很多,这里不再赘述,提供一个常见方式在activity下:
// 隐藏状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
2、一些手机比如华为的机子,大多都有虚拟按键即navigationbar,关于隐藏NavigationBar,网络上大多数的做法是设置一个flag,如下:
View decorView = getWindow().getDecorView();
decorView .setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
这是最简单的隐藏方式,大多数应用中能够达到效果,但是很坑的是当你从边缘上滑的时候,导航栏就出来了,google明确说明这个动作是不能拦截的。
3、所以很多人想能不能在上面盖一层view,把事件拦截了,不让导航栏弹出来,不过由于view的级别不够,导航栏还是不紧不慢的弹出,后来偶然发现了系统错误弹窗能位于Window的最上方覆盖一切,且不会拦截触摸或点击事件,于是给view设置了这个flag,覆盖上这个view之后把导航栏隐藏,试了试下面的代码搞定了:
private void hideNavigationBar() {
int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//隐藏Navigation Bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;//防止Navigation Bar在覆盖view的情况下上弹
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_view, null);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("rocky", "Hi....");
return false;
}
});
WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, //让window占满整个手机屏幕,不留任何边界(border)
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.y = 0;
params.x = 0;
windowManager.addView(view, params);
view.setSystemUiVisibility(flags);
}