1. 程式人生 > >Android打造萬能的對話方塊Dialog(三)

Android打造萬能的對話方塊Dialog(三)

打造萬能的對話方塊Dialog(三)

已經寫了兩篇了,這下來一篇高階點的,仿照AlertDialog封裝我們想要的Dialog的對話方塊
具體那種方式跟適合你,你自己來選,不過如果只是為了簡單實用,我感覺上一篇的封裝方式也許更合適

先看圖,沒圖說個毛線

這裡寫圖片描述

這裡寫圖片描述

簡單介紹

前面也說了,我們這次要玩一個高階的,仿照Android系統提供AlertDialog的來寫。首先、我們怎麼著也得先看一下原始碼吧(這個自己看吧,就不帶領你們來看了)

AlertDialog原始碼總結

1、建立Builder類,這個是為了構建和設定引數的(這裡說明一下,最好是瞭解一下建造者模式


2、建立Dialog所需要的引數類
3、引數和對話方塊進行資料繫結
4、搞定,進行使用就可以(同AlertDialog用法相同)

建立Builder類

public static class Builder {

        private DialogParams P;

        public Builder(Context context){
            // 如果沒有傳預設的就是自定義的主題
            this(context, 0);
        }

        public Builder(Context context,int
themeResId){ P = new DialogParams(context,themeResId); } /** * Set a custom view resource to be the contents of the Dialog. The * resource will be inflated, adding all top-level views to the screen. * * @param layoutResId Resource ID to be inflated. * @return
this Builder object to allow for chaining of calls to set * methods */
public Builder setView(int layoutResId) { P.mView = null; P.mViewLayoutResId = layoutResId; return this; } /** * Sets a custom view to be the contents of the alert dialog. * <p> * When using a pre-Holo theme, if the supplied view is an instance of * a {@link ListView} then the light background will be used. * <p> * <strong>Note:</strong> To ensure consistent styling, the custom view * should be inflated or constructed using the alert dialog's themed * context obtained via {@link #getContext()}. * * @param view the view to use as the contents of the alert dialog * @return this Builder object to allow for chaining of calls to set * methods */ public Builder setView(View view) { P.mView = view; P.mViewLayoutResId = 0; return this; } /** * 設定寬度 */ public Builder setWidth(int width){ P.mWidth = width; return this; } /** * 設定高度 */ public Builder setHeight(int height){ P.mHeight = height; return this; } /** * 顯示動畫 */ public Builder showAnimationFromBottom(){ P.mAnimation = R.style.main_menu_animstyle; P.mGravity = Gravity.BOTTOM; return this; } public Builder setGravity(int gravity){ P.mGravity = gravity; return this; } /** * 顯示動畫 可以自定動畫 */ public Builder showAnimation(int styleResId){ P.mAnimation = styleResId; return this; } public CommonDialog create() { // Context has already been wrapped with the appropriate theme. final CommonDialog dialog = new CommonDialog(P.context, P.themeResId); // 繫結一些引數 dialog.apply(P); return dialog; }

建立引數類

public static class DialogParams{
            public Context context;
            public int themeResId;
            public View mView;
            public int mViewLayoutResId;
            public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
            public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
            public int mAnimation = 0;
            public int mGravity = Gravity.CENTER;

            public DialogParams(Context context, int themeResId) {
                this.context = context;
                this.themeResId = themeResId;
            }
        }

引數和對話方塊進行資料繫結

/**
     * 繫結和設定引數
     */
    private void apply(Builder.DialogParams P) {
        mDialogView = P.mView;// 資源  View

        if(mDialogView == null && P.mViewLayoutResId == 0){
            throw new NullPointerException("大哥請設定佈局!!!");
        }

        mDialogView = View.inflate(P.context,P.mViewLayoutResId,null);

        // 設定佈局
        setContentView(mDialogView);


        // 設定基本引數
        Window window = getWindow();

        window.setLayout(P.mWidth, P.mHeight);

        window.setGravity(P.mGravity);

        if(P.mAnimation != 0) {
            window.setWindowAnimations(P.mAnimation);
        }

    }

使用

findViewById(R.id.btn_center).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CommonDialog.Builder  builder = new CommonDialog.Builder(MainActivity.this,R.style
                        .DialogTheme);
                builder.setView(R.layout.dialog_center)
                        .setGravity(Gravity.CENTER);
                CommonDialog dialog = builder.create();
                dialog.show();

            }
        });

總結

可能很多人會說,為什麼全是程式碼。一點廢話都沒有,我想說的是,也許真的是太簡單了,所以就略過白話問了。如果還是不懂,程式碼奉上。

原始碼下載

原始碼下載