顺便处理了软键盘弹出收起时不能再次隐藏navigation和statusbar的问题
直接上代码:
private LinearLayout llMain;
private int screenHeight;
private WindowManager wm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wm = this.getWindowManager();
//activity最外层layout
llMain = (LinearLayout) findViewById(R.id.llMain);
//这里监听键盘弹出收起状态
llMain
.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
Rect r = new Rect();
llMain.getWindowVisibleDisplayFrame(r);
if (r.bottom == screenHeight) {
fullScreenImmersive();
}
}
});
}
//首次进入时隐藏navigation和statusbar
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (screenHeight == 0) {
screenHeight = wm.getDefaultDisplay().getHeight();
}
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
fullScreenImmersive();
}
}
//沉浸式关键代码
private void fullScreenImmersive() {
if (Build.VERSION.SDK_INT >= 19) {
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
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
网友评论