深层自定义Dialog

作者:XINHAO_HAN
系统默认的Dialog样式不好看,可能根据不同的项目改动也很大,那么如何自定义一个属于自己的Dialog?
来我们先看一下Dialog的样式
代码

  

//@drawable/dialog_back




    
    




//我的APP样式

深层自定义Dialog_第1张图片
Dialog.png

//先继承于一个Dialog起名为随意

public class HxHDialog extends Dialog {

    public HxHDialog(@NonNull Context context) {
        super(context);
    }

    public HxHDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);//更改此处的样式
    }

}

先给你理一下思路

如果我们要更改一个Dialog,基本上都是更改它的Window,我们看看 setContentView的内部方法


    /**
     * Set the screen content to an explicit view.  This view is placed
     * directly into the screen's view hierarchy.  It can itself be a complex
     * view hierarchy.
     * 
     * @param view The desired content to display.
     */
    public void setContentView(@NonNull View view) {
        mWindow.setContentView(view);//此处是给Window设置布局
    }



//看看这
 final Window w = new PhoneWindow(mContext);
        mWindow = w;

//用的是和Activity一样的Window类,自己明白了木有???

但是你像我上边做出来的基本上窗口大小不固定
但是一般的Dialog有一个"问题",就是根据你的需求Dialog宽度要全屏,就像这样


深层自定义Dialog_第2张图片
Dialog_style.png

//一定是要在show之后调用
  @Override
    public void show() {
        super.show();
        getWindow().getDecorView().setPadding(0, 0, 0, 0);..设置padding为0,默认的有padding所以看起来全屏不了
//设置宽和高
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
//在屏幕的那个位置显示
        // getWindow().setGravity(Gravity.TOP);
//设置动画
        getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);
    }

更改Dialog窗口大小

 this.getWindow().setLayout(宽, 高);//必须在show之后调用

如果你想给Dialog加上5.0特效动画

  public void shouWindows(View view) {
        this.showAsDropDown(view);
        this.view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                UIUtils.setViewAnima(XINHAO_HAN_BasePopuWindows.this.view);
                XINHAO_HAN_BasePopuWindows.this.view.removeOnLayoutChangeListener(this);

            }
        });
    }

//  UIUtils.setViewAnima(XINHAO_HAN_BasePopuWindows.this.view);

 //设置动画
    public static void setViewAnima(View view) {

        Animator circularReveal = ViewAnimationUtils.createCircularReveal(view, 0, 0,   
     0, (float) Math.hypot(view.getWidth(), view.getHeight()));
        circularReveal.setDuration(500);
        circularReveal.setInterpolator(new AccelerateInterpolator());
        circularReveal.start();

    }
//就会像画圆一样慢慢扩开视图

就像这样:

持续更新,目前:第一版

2017-11-01-10mzdialog.gif

你可能感兴趣的:(深层自定义Dialog)