1.正常dialog写法,如果有刘海屏,上面是不会顶到最上面,刘海位置空出来了
// 创建Dialog final AlertDialog dialog = new AlertDialog.Builder(activity).create(); Window window = dialog.getWindow(); window.setBackgroundDrawableResource(android.R.color.transparent); // 设置点击dialog以外区域不取消Dialog dialog.setCancelable(true); dialog.show(); dialog.setContentView(view);
2.有刘海顶满屏幕写法
// 创建Dialog final AlertDialog dialog = new AlertDialog.Builder(activity).create(); Window window = dialog.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); } window.setBackgroundDrawableResource(android.R.color.transparent); // 设置点击dialog以外区域不取消Dialog dialog.setCancelable(true); dialog.show(); dialog.setContentView(view);