简体   繁体   English

使用全屏对话框时如何隐藏导航栏

[英]How to Hide Navigation Bar When Using Full Screen Dialog

I am using a full-screen Dialog to show ExoPlayer video in full screen mode.我正在使用全屏Dialog以全屏模式显示ExoPlayer视频。 I am already using the following code in the onCreate method of the Activity hosting the Dialog to hide the Navigation Bar .我已经在托管DialogActivityonCreate方法中使用以下代码来隐藏Navigation Bar

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hide the navigation bar in the beginning
        final View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        //Set a system bar visibility change listener
        decorView.setOnSystemUiVisibilityChangeListener(
                new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            decorView.setSystemUiVisibility(
                                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                        }
                    }
                }
        );


    }
}

The problem is that as soon as the dialog opens the navigation bar appears and I cannot hide it.问题是,一旦对话框打开,导航栏就会出现,我无法隐藏它。

private void openFullScreenDialog() {
    ((ViewGroup) mExoPlayerView.getParent()).removeView(mExoPlayerView);
    mFullScreenDialog.addContentView(mExoPlayerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    mFullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PlaybackActivity.this, R.drawable.ic_fullscreen_exit));
    mExoPlayerFullScreen = true;
    mFullScreenDialog.show();
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}

The navigation bar appears no matter what options I use for the dialog or setSystemUiVisibility() .无论我为对话框或setSystemUiVisibility()使用什么选项,导航栏都会出现。

Do this:做这个:

decorView.setOnSystemUiVisibilityChangeListener(
        new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

            }
        }
);

Also override this method:还覆盖此方法:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        }
    }

Try this尝试这个

The best solution is to use a DialogFragment最好的解决方案是使用 DialogFragment

Show dialog显示对话框

DialogFragment.show(getSupportFragmentManager(), DialogFragment.TAG);

Full screen全屏

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(), R.style.MainDialog) { //set the style, the best code here or with me, we do not change
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            getActivity().finish();
        }
    };
    return dialog;
}

Style风格

<style name="MainDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:background">@null</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

Hide system bar onWindowFocus and show when dismiss the dialog隐藏系统栏onWindowFocus并在dismiss对话框时显示

class FullScreenDialog(context: Context): Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen) {
override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if(hasFocus){
        hideSystemBars()
    }
}

private fun hideSystemBars() {
    window?.let {
        val windowInsetsController =
            ViewCompat.getWindowInsetsController(it.decorView) ?: return
        windowInsetsController.systemBarsBehavior =
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        // Hide both the status bar and the navigation bar
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
    }
}

override fun dismiss() {
    super.dismiss()
    showSystemBar()
}

private fun showSystemBar(){
    window?.let {
        val windowInsetsController =
            ViewCompat.getWindowInsetsController(it.decorView) ?: return
        windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
    }
}

} }

Try this:试试这个:

mExoPlayerView.hideController()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 DialogFragment 和导航库时将对话框显示为全屏或对话框 - Show dialog as full screen or as dialog when using DialogFragment and Navigation Library 在 Kotlin 中显示对话框时如何隐藏片段中的底部导航栏? - How to hide bottom navigation bar in fragment when dialog is displayed in Kotlin? 如何检测系统导航栏何时出现在全屏模式? - How to detect when the system navigation bar appears on full screen mode? 全屏显示时出现Android导航栏 - Android Navigation bar appearing when in full screen 使用导航栏和状态栏设置全屏时,fitsystemwindows被忽略 - When setting Full screen with navigation bar and status bar fitsystemwindows is ignored 全屏对话框在状态栏和导航栏上放置灰色 Jetpack Compose - Full screen Dialog puts grey color on status bar and navigation bar Jetpack Compose 视频播放器在选项卡内处于全屏模式时,如何隐藏操作栏和选项卡布局? - How to hide Action bar and Tab Layout when video player is in full screen mode inside a tab? 当我隐藏底部导航或顶部栏时如何防止延迟屏幕效果? - When i hide bottom navigation or top bar How do I prevent the delayed screen effect? 如何在底部标签栏上方全屏设置对话框? - How to set the dialog in full screen above the bottom tab bar? 微调器打开时如何隐藏导航栏 - How to hide navigation bar when spinner open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM