记录工作中的点点滴滴之Dialog全屏显示适配刘海屏

Android中的Dialog适配刘海屏全屏显示:

Dialog mdialog =new Dialog(mActivity, R.style.MyDialog);

/**全屏展示UI不影响导航栏*/

Window window = mdialog.getWindow();

if (window !=null) {

window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    window.getDecorView().setPadding(0, 0, 0, 0);

    window.getDecorView().setBackgroundColor(Color.WHITE);

    WindowManager.LayoutParams layoutParams = window.getAttributes();

    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

// 延伸显示区域到刘海

        WindowManager.LayoutParams lp = window.getAttributes();

        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;

        window.setAttributes(lp);

        // 设置页面全屏显示

        final View decorView = window.getDecorView();

        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    }

window.setAttributes(layoutParams);

}

//设置自定义dialog,做一些操作

mdialog.setContentView(LayoutInflater.from(mActivity).inflate(R.layout.dialog_private_agreement, null));

//显示

if (!mdialog.isShowing()) {

mdialog.show();

        }

你可能感兴趣的:(记录工作中的点点滴滴之Dialog全屏显示适配刘海屏)