NavigationBarUtil工具类-去除底部导航栏和状态栏
一、作用
去除标题栏和底部导航栏
二、使用
在onCreate()中setContentView加载layout之前使用
NavigationBarUtil.hideNavigationBar(getWindow());
三、注意
1.使用dialog等会使得底部导航栏再次出现,以下是解决办法
//失能焦点
NavigationBarUtil.focusNotAle(dialog.getWindow());
dialog.show();
//显示虚拟栏的时候 隐藏
NavigationBarUtil.hideNavigationBar(dialog.getWindow());
//再清理失能焦点
NavigationBarUtil.clearFocusNotAle(dialog.getWindow());
四、代码
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
/**
* author : GodH
* date : 2019/8/22 15:39
* desc :控制虚拟栏
* version: 1.0
*/
public class NavigationBarUtil {
/**
* 隐藏虚拟栏 ,显示的时候再隐藏掉
*
* @param window
*/
static public void hideNavigationBar(final Window window) {
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
window.getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
//布局位于状态栏下方
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
//全屏
View.SYSTEM_UI_FLAG_FULLSCREEN |
//隐藏导航栏
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (Build.VERSION.SDK_INT >= 19) {
uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
} else {
uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
}
window.getDecorView().setSystemUiVisibility(uiOptions);
}
});
}
static public void showNavigationBar(final Window window) {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = window.getDecorView();
v.setSystemUiVisibility(View.VISIBLE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = window.getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE|
View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
}
}
/**
* dialog 需要全屏的时候用,和clearFocusNotAle() 成对出现
* 在show 前调用 focusNotAle show后调用clearFocusNotAle
*
* @param window
*/
static public void focusNotAle(Window window) {
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
/**
* dialog 需要全屏的时候用,focusNotAle() 成对出现
* 在show 前调用 focusNotAle show后调用clearFocusNotAle
*
* @param window
*/
static public void clearFocusNotAle(Window window) {
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
}