Android默认的显示/隐藏NavigationBar是配置载/system/build.prop
qemu.hw.mainkeys=0/1修改这个值重启即可显示/隐藏。
如果有一个应用想要打开自动隐藏,关闭后在打开。代码很简单,如下:
- int flags;
- int curApiVersion = android.os.Build.VERSION.SDK_INT;
- // This work only for android 4.4+
- if(curApiVersion >= Build.VERSION_CODES.KITKAT){
- // This work only for android 4.4+
- // hide navigation bar permanently in android activity
- // touch the screen, the navigation bar will not show
- flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_IMMERSIVE
- | View.SYSTEM_UI_FLAG_FULLSCREEN;
- }else{
- // touch the screen, the navigation bar will show
- flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
- }
- // must be executed in main thread :)
- getWindow().getDecorView().setSystemUiVisibility(flags);
这里注释看的很清楚,4.4以下版本隐藏是不好用的,随便点以下就出来了。
4.4 第一次调用这样的代码会提示 滑动上下栏退出沉寂模式,实测下拉状态栏或者上拉虚拟按键原来的位置,会看到虚拟按键。
所以要把这一个手势导致看到虚拟按键的地方去掉,
这里定义了一个手势检测的工具最后会调用
requestTransientBars
,删除显示
NavigationBar这里就行了。
这里只针对4.4修改:
1.去除下拉手势中刷新BAR相关的操作。
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
// monitor for system gestures
mSystemGestures = new SystemGesturesPointerEventListener(context,
new SystemGesturesPointerEventListener.Callbacks() {
@Override
public void onSwipeFromTop() {
if (mStatusBar != null) {
requestTransientBars(mStatusBar);
}
}
@Override
public void onSwipeFromBottom() {
if (mNavigationBar != null && mNavigationBarOnBottom) {
requestTransientBars(mNavigationBar);
}
}
@Override
public void onSwipeFromRight() {
if (mNavigationBar != null && !mNavigationBarOnBottom) {
requestTransientBars(mNavigationBar);
}
}
@Override
public void onDebug() {
// no-op
}
});
private void requestTransientBars(WindowState swipeTarget) {
synchronized (mWindowManagerFuncs.getWindowManagerLock()) {
boolean sb = mStatusBarController.checkShowTransientBarLw();
boolean nb = mNavigationBarController.checkShowTransientBarLw();
if (sb || nb) {
WindowState barTarget = sb ? mStatusBar : mNavigationBar;
if (sb ^ nb && barTarget != swipeTarget) {
if (DEBUG) Slog.d(TAG, "Not showing transient bar, wrong swipe target");
return;
}
//if (sb) mStatusBarController.showTransient();
//if (nb) mNavigationBarController.showTransient();
mImmersiveModeConfirmation.confirmCurrentPrompt();
updateSystemUiVisibilityLw();
}
}
}
2.总有些老版的APP兼容情况不好,就开一下挂解决一下。
强制加上
SYSTEM_UI_FLAG_IMMERSIVE
frameworks/base/core/java/android/view/View.java
public void setSystemUiVisibility(int visibility) {
if (visibility != mSystemUiVisibility) {
mSystemUiVisibility = visibility;
/* if you want hide navigation
SYSTEM_UI_FLAG_HIDE_NAVIGATION :touch anywhere will resume show
so:
add TAG: SYSTEM_UI_FLAG_HIDE_NAVIGATION
*/
if(0 != ( mSystemUiVisibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)) {
mSystemUiVisibility |= SYSTEM_UI_FLAG_IMMERSIVE;
}
if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
mParent.recomputeViewAttributes(this);
}
}
}