设置DialogFragment背景透明的方法如下:
1.在onCreateView()方法中设置弹窗内部的背景透明。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
// 设置弹窗内部的背景透明
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return inflater.inflate(R.layout.fragment_dialog_test, container, false);
}
2.在onStart()方法中设置弹窗外部的背景透明度。
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
// 设置弹窗外部的背景透明度,需要配合FLAG_DIM_BEHIND一起使用。
// dimAmount取值范围为0f~1f,0f表示完全透明,1f表示完全不透明。
params.dimAmount = 0.6f;
// 让该window覆盖的所有页面显示都变暗淡
params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(params);
}