相信肯定有不少人在Android开发中使用AlertDialog的时候,遇到AlertDialog总是有边距的问题。下面就来介绍如何使AlertDialog没有边距。(只以设置AlertDialog的宽度占满屏幕宽度为例,高度情况类似,请自行实现)
先上效果图:
左右两边有边距是因为AlertDialog依赖的父布局设置的有左右边距分别为10dp,所以显示的dialog才会有左右边距。这里可能有人会说,不想让dialog有任何边距的显示,我想到的方法有两个:
1:将AlertDialog的父布局的左右边距设置为0dp
2:获取当前手机屏幕的宽度,如下设置:
这个是这篇博文的核心部分,其他可以不用看
window.getDecorView().setPadding(0, 0, 0, 0);
//从Android 4.1开始向上兼容,对下不兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
window.getDecorView().setBackground(context.getResources().getDrawable(R.drawable.dialog_top_bg));
}
上面这段代码要在下面这段代码之前设置,则无边距。如果在下面这段代码之后设置,则会产生跟边距(等于父布局的边距)
lp.width = (int) (WindowManager.LayoutParams.MATCH_PARENT);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
其中设置:decorView.setBackground();// 这个方法必须设置,里边的资源文件是设置dialog的背景,后面贴出
背景资源的代码:
以上两种方法,可供选择;下面贴出我写的一个AlertDialog的代码,可做工具类使用,布局可自由传入,比较灵活:
/**
* Created by **
* on 2018/10/10.
* 所有的dialog集中在这里
*/
public class DialogUtils {
@SuppressLint("StaticFieldLeak")
private static DialogUtils mDialogUtils;
public static DialogUtils instance() {
if (mDialogUtils == null) {
synchronized (DialogUtils.class) {
if (mDialogUtils == null) {
mDialogUtils = new DialogUtils();
}
}
}
return mDialogUtils;
}
//设置dialog的布局
private View mView;
public DialogUtils setView(View view) {
mView = view;
return this;
}
private Integer mGravity;
//设置dialog的位置
public DialogUtils setGravity(Integer gravity) {
mGravity = gravity;
return this;
}
//设置dialog的动画属性
private Integer mDialogAnimStyle;
public DialogUtils setDialogAnimStyle(Integer dialogAnimStyle) {
mDialogAnimStyle = dialogAnimStyle;
return this;
}
//设置dialog大小的参数
private Float mDialogW = 1f;
private DialogUtils setDialogW(Float dialogW) {
mDialogW = dialogW;
return this;
}
private Float mDialogH;
private DialogUtils setDialogH(Float dialogH) {
mDialogH = dialogH;
return this;
}
//设置点击dialog外围是否取消
private Boolean mIsCancel = false;
public DialogUtils setIsCancel(Boolean isCancel) {
mIsCancel = isCancel;
return this;
}
//设置dialog的属性
private Integer mDialogStyle;
public DialogUtils setDialogStyle(Integer dialogStyle){
mDialogStyle=dialogStyle;
return this;
}
public Dialog gMDialog(Activity activity,Context context) {
AlertDialog.Builder builder;
AlertDialog alertDialog;
if (mDialogStyle!=null){
builder = new AlertDialog.Builder(context, mDialogStyle);
}else {
builder = new AlertDialog.Builder(context);
}
alertDialog = builder.create();
Window window = alertDialog.getWindow();
assert window != null;
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int) (WindowManager.LayoutParams.MATCH_PARENT / mDialogW);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//设置位置
window.setGravity(mGravity);
//设置window动画
if (mDialogAnimStyle!=null){
window.setWindowAnimations(mDialogAnimStyle);
}
//可根据效果的需要替换上面的那段代码
// WindowManager windowManager = activity.getWindowManager();
// Display defaultDisplay = windowManager.getDefaultDisplay();
// Point outSize=new Point();
// defaultDisplay.getSize(outSize);
//
// Window win = alertDialog.getWindow();
// assert win != null;
// View decorView = win.getDecorView();
// decorView.setPadding(0,0,0,0);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// decorView.setBackground(context.getResources().getDrawable(R.drawable.dialog_top_bg));
// }
// win.setGravity(mGravity);
// WindowManager.LayoutParams lp = win.getAttributes();
// Log.d("333",outSize.x+"");
// lp.width =outSize.x;
// lp.height=WindowManager.LayoutParams.WRAP_CONTENT;
// win.setAttributes(lp);
// Log.d("333",win.getAttributes().width+" : ==========888");
window.getDecorView().setPadding(0, 0, 0, 0);
//从Android 4.1开始向上兼容,对下不兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
window.getDecorView().setBackground(context.getResources().getDrawable(R.drawable.dialog_top_bg));
}
window.setAttributes(lp);
//给dialog设置布局
alertDialog.setView(mView);
//设置点击外围取消
alertDialog.setCanceledOnTouchOutside(mIsCancel);
alertDialog.setCancelable(mIsCancel);
//展示dialog
alertDialog.show();
return alertDialog;
}
}
小主儿,喜欢就点个赞呗!!! ^_^