深層自定義Dialog

作者:XINHAO_HAN
系統(tǒng)默認(rèn)的Dialog樣式不好看,可能根據(jù)不同的項(xiàng)目改動也很大,那么如何自定義一個(gè)屬于自己的Dialog?
來我們先看一下Dialog的樣式
代碼

  <style name="MyDialog" parent="Base.V11.Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@drawable/dialog_back</item> <!-- 如果要改變背景顏色請更改此處,我這個(gè)默認(rèn)加了弧度-->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:backgroundDimEnabled">true</item> <!-- 背景模糊 -->
    </style>

//@drawable/dialog_back

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#fff"></solid>
    <corners android:radius="8dp"
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"></corners>


</shape>

//我的APP樣式

Dialog.png

//先繼承于一個(gè)Dialog起名為隨意

public class HxHDialog extends Dialog {

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

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

}

先給你理一下思路

如果我們要更改一個(gè)Dialog,基本上都是更改它的Window,我們看看 setContentView的內(nèi)部方法


    /**
     * 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設(shè)置布局
    }



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

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

但是你像我上邊做出來的基本上窗口大小不固定
但是一般的Dialog有一個(gè)"問題",就是根據(jù)你的需求Dialog寬度要全屏,就像這樣


Dialog_style.png

//一定是要在show之后調(diào)用
  @Override
    public void show() {
        super.show();
        getWindow().getDecorView().setPadding(0, 0, 0, 0);..設(shè)置padding為0,默認(rèn)的有padding所以看起來全屏不了
//設(shè)置寬和高
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
//在屏幕的那個(gè)位置顯示
        // getWindow().setGravity(Gravity.TOP);
//設(shè)置動畫
        getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);
    }

更改Dialog窗口大小

 this.getWindow().setLayout(寬, 高);//必須在show之后調(diào)用

如果你想給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);

 //設(shè)置動畫
    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();

    }
//就會像畫圓一樣慢慢擴(kuò)開視圖

就像這樣:

持續(xù)更新,目前:第一版

2017-11-01-10mzdialog.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容