Launcher开发的时候有个需求是长按图标时,要隐藏状态栏弹出卸载框。但是发现隐藏状态栏的时候workspace会整体往上挪,要想隐藏状态栏时布局不变,需要先在主题属性里加两个属性就好了:
<style name="Theme" parent="@android:style/Theme.Holo.Wallpaper.NoTitleBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
代码里隐藏和显示状态栏的代码:
public void showStatusBar() {
// if (mLauncherView != null) {
// mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.VISIBLE);
// }
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}
public void hideStatusBar() {
// if (mLauncherView != null) {
// mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.INVISIBLE);
// }
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}
上面的函数在 android7.1上已经无效了,布局还是会上移。7.1后使用下面可以使用下面的函数:
public void showStatusBar() {
int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
uiFlags |= 0x00001000;
getWindow().getDecorView().setSystemUiVisibility(uiFlags);
Log.d("Launcher_status_bar", "Launcher showStatusBar()");
}
public void hideStatusBar() {
int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
uiFlags |= 0x00001000;
getWindow().getDecorView().setSystemUiVisibility(uiFlags);
Log.d("Launcher_status_bar", "Launcher hideStatusBar()");
}