显示警报对话框 Android 时隐藏状态栏 [英] Hiding Status Bar while showing Alert Dialog Android

查看:46
本文介绍了显示警报对话框 Android 时隐藏状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,当您点击某个按钮时会弹出一个警告对话框.状态栏需要隐藏,所以我的活动中有一个方法:

I'm making an app where an alert dialog pops up when you hit a certain button. The status bar needs to be hidden, so I have a method in my activity:

private void hideStatusBar(){
    if (Build.VERSION.SDK_INT < 16){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

我在 Activity 的 onCreate 方法中调用了这个方法,它运行良好,直到弹出警告对话框.一旦显示警报对话框,状态栏就会返回.我尝试了以下方法:

I call this method in the activity's onCreate method, and it works fine until the alert dialog pops up. As soon as the alert dialog is shown, the status bar comes back. I tried the following:

alertDialog.show();
hideStatusBar();

这不起作用.然后我为我的活动覆盖了 onWindowFocusChanged 方法:

which didn't work.Then I overrode the onWindowFocusChanged method for my activity:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideStatusBar();
}

这使状态栏的背景透明,但仍然没有隐藏它.有没有办法在显示警告对话框时隐藏状态栏?

which makes the background of the status bar transparent, but still doesn't hide it. Is there any way to keep the status bar hidden when the alert dialog is shown?

推荐答案

每个对话框都有自己的窗口,有自己的风格.

Each dialog has its own Window which have its own style.

在你的情况下 hideStatusBar() 不起作用,因为它是从活动的 onCreate() 调用的,这意味着它试图改变活动窗口的外观,而不是对话框的窗口.

In your case hideStatusBar() doesn't work because its called from activity's onCreate() it means it tries to change appearance of activity's window, but not the dialog's window.

解决办法是:

子类 AlertDialog.移到那里 hideStatusBar() 并从对话框的 onCreate() 调用它.

Subclass AlertDialog. Move there hideStatusBar() and call it from dialog's onCreate().

这意味着你必须处理 Dialog.getWindow() 而不是 Activity.getWindow()

It means you have to have a deal with Dialog.getWindow() rather Activity.getWindow()

这里有一个小例子:

public static class TranslucentDialog extends AlertDialog {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}

这篇关于显示警报对话框 Android 时隐藏状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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