1. 程式人生 > >設定AlertDialog的佔滿螢幕

設定AlertDialog的佔滿螢幕

相信肯定有不少人在Android開發中使用AlertDialog的時候,遇到AlertDialog總是有邊距的問題。下面就來介紹如何使AlertDialog沒有邊距。(只以設定AlertDialog的寬度佔滿螢幕寬度為例,高度情況類似,請自行實現)

先上效果圖:

有人可能會說:還是右邊距呀,哈哈,大哥往下看。。。

左右兩邊有邊距是因為AlertDialog依賴的父佈局設定的有左右邊距分別為10dp,所以顯示的dialog才會有左右邊距。這裡可能有人會說,不想讓dialog有任何邊距的顯示,我想到的方法有兩個

1:將AlertDialog的父佈局的左右邊距設定為0dp

2:獲取當前手機螢幕的寬度,如下設定:

      其中設定: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);         } //下面註釋的這段是不受父佈局邊距影響的設定dialog邊距的程式碼,可根據效果的需要替換上面的那段程式碼 //        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;     } } 小主兒,喜歡就點個讚唄!!!    ^_^