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

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

问题描述

我有一个使用片段做出了Android应用程序

I have an Android Application that is made using Fragments

我躲在我的屏幕的顶部和底部的酒吧,使用以下code。

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);
            }
        });
    }
}

当它显示的软键盘条显示,我可以用此居住作为他们隐藏在键盘dismissed.However如果对话片段是节目的同时显示软键盘,然后当两个键盘和对话片段是驳回他们栏保持在应用程序的顶部。

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.

我的三个问题是

是否有可能停止对softkeyboard改变UI模式?

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

是否有可能从改变用户界面模式停止DialogsFraments的表现?

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

编辑:我用低于code,看是否显示在键盘

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;
    }
}

- >我知道有一个变通的一个活动对话,但是我不能看到一个或返工code在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?

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

如果您正在构建一个电子书阅读器,新闻阅读器,或一本杂志,用
  与SYSTEM_UI_FLAG_FULLSCREEN一起身临其境的国旗和
  SYSTEM_UI_FLAG_HIDE_NAVIGATION。因为用户可能要访问的
  操作栏和其他UI控件有点频繁,但不
  不屑与任何UI元素的同时,通过内容翻转,
  身临其境是该用例一个不错的选择。

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。

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

从<一个取我的例子href=\"https://developer.android.com/samples/ImmersiveMode/src/com.example.android.immersivemode/ImmersiveModeFragment.html\"相对=nofollow> ImmersiveModeFragment.java 样本。

@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()

这是在这里拍摄不粘腻浸泡

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天全站免登陆