1、显示布局大小不正确--设置尺寸
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 810;
dialog.getWindow().setAttributes(params);
2、有圆角显示,并不是原始背景图片
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setType(WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 24);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
3、setCancelable和setCanceledOnTouchOutside的区别
默认值都是true
setCancelable:设置点击外部遮罩和返回键,dialog是否消失
setCanceledOnTouchOutside:设置点击外部遮罩是否可以关闭dialog(返回键仍然生效)
4、设置dialog显示位置
window.setGravity(Gravity.TOP | Gravity.CENTER);
5、设置遮罩不显示透明
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setDimAmount(0f);
6、设置dialog下层点击事件生效
//允许点击事件
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
7、遮盖状态栏
这里就不能再使用dialog去实现,需要使用WindowManager实现;
public void initView() {
if (mManager == null) {
mManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
}
mView = View.inflate(this, R.layout.layout_demo, null);
mProgress = mView.findViewById(R.id.progress_data);
mBar = mView.findViewById(R.id.seek_bar);
}
public void showView() {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
//错误弹窗,层级很高
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
//设置下层可点击
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置显示位置
layoutParams.gravity = Gravity.TOP;
//设置遮罩透明
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.width = 860;
layoutParams.height = 130;
layoutParams.y = 0;
// 将悬浮窗控件添加到WindowManager
mManager.addView(mView, layoutParams);
showing = true;
}
8、设置dialog大小不生效
最开始使用的是ConstraintLayout,设置高度大小无效。需要将布局文件改为RelativeLayout。如果使用线性布局LinearLayout会导致自定义xml的大小(宽高)无法调节。
9、dialog的show方法报权限问题
第一步:添加权限判断设置不同的type
if (Build.VERSION.SDK_INT >= 25) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
oneDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}
} else {
oneDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
第二步:授予系统权限
private void checkMyPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
}else {
// SYSTEM_ALERT_WINDOW permission not granted...
Toast.makeText(this, "未被授予权限,相关功能不可用", Toast.LENGTH_SHORT).show();
}
}
}
}
第三步,添加mainfast文件内容
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
10、dialog不显示
布局文件不能使用ConstraintLayout,会导致显示的大小有问题。
11、Dialog弹出时状态栏颜色不改变
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
12、设置dialog弹出时 状态栏颜色
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
window.setDimAmount(0f);