关键属性android:backgroundDimEnabled,设置为true时候,整个Dialog的大背景就是半透明的黑色,如果设置为false就是全透明
1、设置透明度(Dialog自身的透明度)
WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.alpha = 1.0f;
dialog.getWindow().setAttributes(lp);
alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明,自身Dialog不可见。
2、设置黑暗度(Dialog 窗口背景的黑暗度)
WindowManager.LayoutParams lp= dialog.getWindow().getAttributes();
lp.dimAmount = 1.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
true
0.5
dimAmount在0.0f和1.0f之间,0.0f完全不暗,即背景是可见的 ,1.0f时候,背景全部变黑暗
3.设置Dialog底背景模糊和黑暗度
WindowManager.LayoutParams.FLAG_BLUR_BEHIND(设置模糊)
WindowManager.LayoutParams.FLAG_DIM_BEHIND(设置暗淡)
4.清除Dialog底背景模糊和黑暗度
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND)
创建DialogFragment有2种方式,onCreateDialog和onCreateView
建议使用onCreateView,
onCreateDialog总是遇到一些奇葩问题
和Fragment的一样,返回view
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View view = inflater.inflate(R.layout.qr_layout, null);
binding = QrLayoutBinding.bind(view);
return view;
}
有2种方式
在onCreate中
setStyle(STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
或者
setStyle(STYLE_NORMAL,R.style.Dialog_FullScreen);
此种方式是onCreateView中所加载的View全屏,不妨设置view的背景一看,
去掉setStyle
在onStart中设置window参数
Window window = requireDialog().getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.CENTER;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
也可以设置View的全屏
如果view不需要全屏呢?
view 不全屏,系统就会在view周围显示半透明蒙层效果,如果view没设置背景颜色的话,view背景就是白色
如果需要去掉默认白色背景呢,那就需要设置BackgroundDrawable
在onActivityCreated中添加
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
setBackgroundDrawable此代码在onCreateView,onActivityCreated,onStart中都可以添加,因为getDialog已经在onCreateDialog中生成,onCreateDialog生命周期在onCreataView之前,onCreate之后
在onCreateDialog中创建AlertDialog
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.qr_layout, null);
binding = QrLayoutBinding.bind(view);
builder.setView(view);
return builder.create();
}
此时无论在onCreate还是AlertDialog设置style是都不能使View全屏,系统就会在view周围显示半透明蒙层效果,这时只显示View的warp_content大小,
如果不设置getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)),
Dialog是和屏幕边缘有间距的,并不能达到屏幕的宽度
而且在华为P30上,view显示水平无法居中
可以在onStart中重新设置window的width参数解决这个问题
Window window = requireDialog().getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.verticalMargin = 0.0f;
lp.gravity = Gravity.CENTER;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.dimAmount = 0.7f;
window.setAttributes(lp);
假如设置View全屏呢,需要设置该View的参数(如在onCreateDialog,onActivityCreated,onStart中)
binding.layoutCode.setLayoutParams(new LinearLayout.LayoutParams(ScreenManager.getRealWidth(),ScreenManager.getRealHeight()));
直接在DialogFragment中onStart()中设置,因为Dialog是在onStart中show的
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null && getActivity() != null) {
//设置窗口的背景
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.0f;//设置窗口后面的暗度
// lp.alpha = 0.8f;//设置dialog窗口的透明度
//设置dialog窗口的宽
lp.width = ScreenUtil.getScreenWidth(Objects.requireNonNull(getActivity())) *0.75;
lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;//设置dialog窗口的高
dialog.getWindow().setAttributes(lp);
}
}
当然也可以这样
dialog.getWindow().setLayout(400,300);//设置窗口的宽高
dialog.getWindow().setDimAmount(0.5f); //设置窗口后面的暗度
参考:
Dialog整个窗体背景设置为透明
Android Dialog透明度和暗度