显示 DialogFragment 时保持沉浸模式 [英] Maintain Immersive mode when DialogFragment is Shown

查看:87
本文介绍了显示 DialogFragment 时保持沉浸模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Fragment 制作的 Android 应用程序

I have an Android Application that is made using Fragments

我使用以下代码隐藏屏幕顶部和底部的栏.

I am hiding the bars at top and bottom of my screen, using the following code.

 @Override
protected void onResume() {
    super.onResume();
    isInBackground = false;

    if(null == getFragmentManager().findFragmentById(R.id.content_container))
    {
        getFragmentManager().beginTransaction().add(R.id.content_container,new PresenterFragment(), PresenterFragment.FRAG_TAG).commit();
    }

    if(Build.VERSION.SDK_INT >=19)
    {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                decorView.setSystemUiVisibility(uiOptions);
            }
        });
    }
}

当软键盘显示条显示时,我可以忍受它,因为它们在键盘关闭时隐藏.但是,如果在显示软键盘时显示对话框片段,那么当键盘和对话框片段都显示时被驳回,他们的栏仍然在应用程序的顶部.

When the soft keyboard it shown the bars show, I can live with this as they hide when the keyboard is dismissed.However if a dialog Fragment is show while the soft keyboard is shown then when both the keyboard and the dialog fragment are dismissed they bars remain over the top of the application.

我的三个问题是

是否可以停止软键盘来更改 UI 模式?

Is it possible to stop the softkeyboard for changing the UI mode?

是否可以停止显示 DialogsFraments 更改 UI 模式?

Is it possible to stop the showing of DialogsFraments from changing the UI mode?

我曾经在下面的代码中查看是否显示键盘

edit: I used to below code to see if the keyboard is shown

public static boolean isKeyBoardShown(){
    InputMethodManager imm = (InputMethodManager)currentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isAcceptingText()) {
        return true;
    } else {
        return false;
    }
}

-> 我知道在一个 Activity 中有一个解决对话框的方法,但我看不到一个或重新编写代码以在 DialogFragment 中工作

-> I know that there is a work around for dialogs in an activity but I can't see one or rework the code to work in a DialogFragment

如果两者都不可能,当同时显示键盘和 DialogFrament 时,为什么应用会卡在错误的 UI 模式中?

If neither is possible why does the app get stuck in the wrong UI mode when there is both a shown keyboard and DialogFrament?

推荐答案

1.解决方案说明.

我从 android api 文档中引用了以下引用.

I have taken the following quotes from the android api docs.

使用沉浸式全屏模式

启用沉浸式全屏模式后,您的活动将继续接收所有触摸事件.用户可以显示系统栏沿着系统栏通常出现的区域向内滑动.这会清除 SYSTEM_UI_FLAG_HIDE_NAVIGATION 标志(以及SYSTEM_UI_FLAG_FULLSCREEN 标志,如果应用)所以系统栏变成可见的.这也会触发你的View.OnSystemUiVisibilityChangeListener,如果设置.

When immersive full-screen mode is enabled, your activity continues to receive all touch events. The user can reveal the system bars with an inward swipe along the region where the system bars normally appear. This clears the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag (and the SYSTEM_UI_FLAG_FULLSCREEN flag, if applied) so the system bars become visible. This also triggers your View.OnSystemUiVisibilityChangeListener, if set.

首先,使用粘性沉浸时不需要OnSystemUiVisibilityChangeListener.

Firstly, you don't need an OnSystemUiVisibilityChangeListener when using sticky immersion.

但是,如果您希望系统栏再次自动隐藏片刻之后,您可以改为使用SYSTEM_UI_FLAG_IMMERSIVE_STICKY 标志.注意粘性"版本标志不会触发任何侦听器,因为系统暂时禁止显示在此模式下处于瞬态.

However, if you'd like the system bars to automatically hide again after a few moments, you can instead use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag. Note that the "sticky" version of the flag doesn't trigger any listeners, as system bars temporarily shown in this mode are in a transient state.

使用粘性/非粘性沉浸的建议:

如果您正在构建一个真正身临其境的应用,您希望用户能够在屏幕边缘附近互动,你不希望他们需要频繁访问系统 UI,使用 IMMERSIVE_STICKY 标志与 SYSTEM_UI_FLAG_FULLSCREEN 和SYSTEM_UI_FLAG_HIDE_NAVIGATION.例如,这种方法可能是适用于游戏或绘图应用.

If you're building a truly immersive app, where you expect users to interact near the edges of the screen and you don't expect them to need frequent access to the system UI, use the IMMERSIVE_STICKY flag in conjunction with SYSTEM_UI_FLAG_FULLSCREEN and SYSTEM_UI_FLAG_HIDE_NAVIGATION. For example, this approach might be suitable for a game or a drawing app.

但是,您提到用户需要键盘,所以我建议这样做:
使用非粘性沉浸

However, you mention users needing the keyboard, so I suggest this:
Use Non-Sticky Immersion

如果您要构建图书阅读器、新闻阅读器或杂志,请使用IMMERSIVE 标志与 SYSTEM_UI_FLAG_FULLSCREEN 和SYSTEM_UI_FLAG_HIDE_NAVIGATION.因为用户可能想要访问操作栏和其他 UI 控件有点频繁,但不是在翻阅内容时对任何 UI 元素感到困扰,IMMERSIVE 是此用例的不错选择.

If you're building a book reader, news reader, or a magazine, use the IMMERSIVE flag in conjunction with SYSTEM_UI_FLAG_FULLSCREEN and SYSTEM_UI_FLAG_HIDE_NAVIGATION. Because users may want to access the action bar and other UI controls somewhat frequently, but not be bothered with any UI elements while flipping through content, IMMERSIVE is a good option for this use case.

<小时>

2.解决方案

我的解决方案是在片段的 onActivityCreated 中设置视图 ui.

My solution is to set up your view ui in the onActivityCreated of your fragments.

我的示例取自 ImmersiveModeFragment.java 示例.

My example taken from ImmersiveModeFragment.java sample.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final View decorView = getActivity().getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(
            new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int i) {
                    hideSystemUI();
                }
            });
}

创建一个单独的方法来管理您从 OnSystemUiVisibilityChangeListener()

Create a separate method to manage the ui that you call from your OnSystemUiVisibilityChangeListener()

取自这里非粘性沉浸

private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    mDecorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
        | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

然后你可以再次调用这个方法onResume.

You can then call this method again onResume.

onResume(){
    hideSystemUI();
}

<小时>

3.替代解决方案.

如果粘性沉浸是您真正想要的,您需要尝试不同的方法.

If sticky immersion is what you really want you need to try a different approach.

用于粘性沉浸

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
}

希望这能解决您的问题.

I hope this solves your problems.

这篇关于显示 DialogFragment 时保持沉浸模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆