gpt4 book ai didi

android - 如何在对话框中保持沉浸式模式?

转载 作者:IT老高 更新时间:2023-10-28 13:05:14 25 4
gpt4 key购买 nike

当我的 Activity 显示自定义对话框时,如何保持新的沉浸式模式?

我正在使用下面的代码来维护对话框中的沉浸式模式,但使用该解决方案时,当我启动自定义对话框时,导航栏出现不到一秒钟,然后消失。

以下视频更好地解释了该问题(在出现导航栏时查看屏幕底部):http://youtu.be/epnd5ghey8g

如何避免这种行为?

代码

我的应用程序中所有 Activity 的父亲:

public abstract class ImmersiveActivity extends Activity {

@SuppressLint("NewApi")
private void disableImmersiveMode() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN
);
}
}

@SuppressLint("NewApi")
private void enableImmersiveMode() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
);
}
}


/**
* Set the Immersive mode or not according to its state in the settings:
* enabled or not.
*/
protected void updateSystemUiVisibility() {
// Retrieve if the Immersive mode is enabled or not.
boolean enabled = getSharedPreferences(Util.PREF_NAME, 0).getBoolean(
"immersive_mode_enabled", true);

if (enabled) enableImmersiveMode();
else disableImmersiveMode();
}

@Override
public void onResume() {
super.onResume();
updateSystemUiVisibility();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
updateSystemUiVisibility();
}

}


我所有的自定义对话框都在其 onCreate(. . .) 方法中调用此方法:

/**
* Copy the visibility of the Activity that has started the dialog {@link mActivity}. If the
* activity is in Immersive mode the dialog will be in Immersive mode too and vice versa.
*/
@SuppressLint("NewApi")
private void copySystemUiVisibility() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
mActivity.getWindow().getDecorView().getSystemUiVisibility()
);
}
}


编辑 - 解决方案(感谢 Beaver6813,查看他的答案了解更多详情):

我所有的自定义对话框都以这种方式覆盖 show 方法:

/**
* An hack used to show the dialogs in Immersive Mode (that is with the NavBar hidden). To
* obtain this, the method makes the dialog not focusable before showing it, change the UI
* visibility of the window like the owner activity of the dialog and then (after showing it)
* makes the dialog focusable again.
*/
@Override
public void show() {
// Set the dialog to not focusable.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

copySystemUiVisibility();

// Show the dialog with NavBar hidden.
super.show();

// Set the dialog to focusable again.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}

最佳答案

在对该问题进行大量研究后,有一个解决此问题的骇人听闻的方法,其中涉及拆开 Dialog类找到。将对话框窗口添加到窗口管理器时会显示导航栏,即使您在将其添加到管理器之前设置了 UI 可见性也是如此。在 Android Immersive example有人评论说:

// * Uses semi-transparent bars for the nav and status bars
// * This UI flag will *not* be cleared when the user interacts with the UI.
// When the user swipes, the bars will temporarily appear for a few seconds and then
// disappear again.

我相信这就是我们在这里看到的(当一个新的、可聚焦的窗口 View 添加到管理器时,会触发用户交互)。

我们如何解决这个问题?在我们创建 Dialog 时使其不可聚焦(因此我们不会触发用户交互),然后在它显示后使其可聚焦。

//Here's the magic..
//Set the dialog to not focusable (makes navigation ignore us adding the window)
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

//Show the dialog!
dialog.show();

//Set the dialog to immersive
dialog.getWindow().getDecorView().setSystemUiVisibility(
context.getWindow().getDecorView().getSystemUiVisibility());

//Clear the not focusable flag from the window
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

显然这并不理想,但它似乎是一个 Android 错误,他们应该检查 Window 是否具有沉浸式设置。

我已将我的工作测试代码更新为 Github .我已经在 Nexus 5 模拟器上进行了测试,它可能会因任何低于 KitKat 的东西而崩溃,但它仅用于概念验证。

关于android - 如何在对话框中保持沉浸式模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22794049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com