显示 DialogFragment 时隐藏系统导航栏

我的应用程序是sticky immersive mode因为system navigation bar(我称之为 SNB)隐藏在我的应用程序的运行时间中。它运行良好但存在一个问题,就是每次我显示 a 时DialogFragment,都会出现 SNB。我怎么能隐藏它。


我在活动类中编写了几行代码,使我的应用程序变为全屏和sticky immersive mode.


MyActivity.java


@Override

protected void onResume() {

    super.onResume();

    hideSystemUI();

}


@Override

public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    hideSystemUI();

}


public void hideSystemUI() {

    // Enables regular immersive sticky mode.

    View decorView = getWindow().getDecorView();

    decorView.setSystemUiVisibility(

            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

                    );

}

MyDialog.java


public class MyDialog extends DialogFragment{


    @NonNull

    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        AlertDialog alertD = builder.create();

        alertD.setView(view);

        return alertD;

    }


    public void show() {

        Activity activity = getActivity();

        show(activity.getSupportFragmentManager(), "dialog");

    }


    @Override

    public void onResume() {

        super.onResume();

        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();

        params.width = ViewGroup.LayoutParams.MATCH_PARENT;

        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);

    }

}

我按照答案显示对话框而不显示 SNB:https ://stackoverflow.com/a/33589793/10467639但它没有按预期工作


MyDialog dialog = new MyDialog();

// Set the dialog to not focusable.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

dialog.show();

// Set the dialog to focusable again.

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);


四季花海
浏览 314回答 2
2回答

慕妹3242003

按照这个答案https://stackoverflow.com/a/23207365/8531215,我已经测试过并且工作正常!onCreateDialog()稍微修改一下方法public Dialog onCreateDialog(Bundle savedInstanceState) {        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());        AlertDialog alertD = builder.create();        alertD.setView(view);        Dialog dialog = alertD.create();        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);        //Show the dialog!        dialog.show();         //Set the dialog to immersive sticky mode        dialog.getWindow().getDecorView().setSystemUiVisibility(                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);        //Clear the not focusable flag from the window        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);        return alertD;}

精慕HU

根据这里的文档:实施 onWindowFocusChanged()。如果获得窗口焦点,您可能需要重新隐藏系统栏。如果您失去窗口焦点,例如由于对话框或弹出菜单显示在您的应用程序上方,您可能想要取消您之前使用 Handler.postDelayed() 或类似的东西安排的任何未决的“隐藏”操作。所以我建议你做的是,改变你的代码MyActivity.java:@Overridepublic void onWindowFocusChanged(boolean hasFocus) {    super.onWindowFocusChanged(hasFocus);    hideSystemUI();}到:@Overridepublic void onWindowFocusChanged(boolean hasFocus) {     if (!hasFocus){            //we lost focus due to DialogFragment            hideSystemUI();                    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java