我一直在到处寻找,但我对此一无所知:试图在项目中激活沉浸式模式;几乎一切正常,除了我的状态栏的背景总是留在那里,破坏了沉浸感......我已经包含了激活沉浸模式之前和之后的屏幕截图,并将“colorPrimaryDark”设置为全绿色以获得最大对比度: 屏幕截图显示了状态栏的背景,什么都没有 我在空白项目中使用并重新插入以隔离此问题的代码直接来自谷歌开发示例,在我的 MainActivity 中,我有:
private final String TAG = "DEBUG::" + this.getClass().getSimpleName();
private final int INITIAL_HIDE_DELAY = 1500;
private View decorView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//setting needed decorView for fullscreen behavior
decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(onSystemUiVisibilityChangeListener);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.i(TAG, "onWindowFocusChanged::hasFocus = " + hasFocus);
if (hasFocus) {// When the window gains focus, hide the system UI.
delayedHide(INITIAL_HIDE_DELAY);
} else {// When the window loses focus, cancel any pending hide action.
mHideHandler.removeMessages(0);
}
}
private void hideSystemUI() {
Log.i(TAG, "hideSystemUI");
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
}
private final Handler mHideHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
hideSystemUI();
}
};
private void delayedHide(int delayMillis) {
Log.i(TAG, "delayedHide");
mHideHandler.removeMessages(0);
mHideHandler.sendEmptyMessageDelayed(0, delayMillis);
}
private View.OnSystemUiVisibilityChangeListener onSystemUiVisibilityChangeListener =
new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// The system bars are visible
getSupportActionBar().show();
delayedHide(INITIAL_HIDE_DELAY);
} else {
// The system bars are NOT visible
getSupportActionBar().hide();
}
}
};
我想知道我的问题是否来自布局或样式文件,但这些是来自项目生成的原始文件......我希望有人能指出我失败的地方!提前致谢!
编辑:我发现android:fitsSystemWindows="true"
从我的活动的布局文件中删除:允许真正的全屏模式,但是,我的 ActionBar 部分隐藏在 StatusBar 后面 - 当显示时。会不会是我设置getSupportActionBar().show();
的时候onSystemUiVisibilityChangeListener
,画得太早了?
编辑 2: 到目前为止,我的理解是,关于我的内容(包括操作栏)的位置/大小,我只有 2 个选择:
屏幕顶部,它将显示被状态栏部分隐藏的操作栏,
或者在状态栏的下方底部,当状态栏被隐藏时,这会给我留下一个“洞”-_-
我现在正在寻找一种解决方案,在我的onSystemUiVisibilityChangeListener
方法中自己在屏幕外/屏幕上设置 ActionBar 的动画,但找不到方法为了抓住它的观点,在那里发布的解决方案https://stackoverflow.com/a/21125631/6463888似乎已经过时了......