[英]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
.我已经在托管Dialog
的Activity
的onCreate
方法中使用以下代码来隐藏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.