Android App实现全屏显示隐藏标题栏与虚拟按键

By | 2017年4月27日

Android APP需要在启动后默认全屏,头部的标题栏,有些还会在底部显示虚拟按键,使用下面方法可以隐藏起来:

private void hide() {
    //隐藏顶部的标题栏
    this.getSupportActionBar().hide();
    //隐藏虚拟按键,并且全屏
    if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
        View v = this.getWindow().getDecorView();
        v.setSystemUiVisibility(View.GONE);
    } else if (Build.VERSION.SDK_INT >= 19) {
        //for new api versions.
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

在onCreate,onResume时调用即可:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.hide();
    this.setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
    this.hide();
    super.onResume();
}