Android周記四:簡單封裝一個底部彈出框BottomDialog

前言

以前總以為這個底部彈出的視圖是在普通的視圖中添加的,所以沒太在意它的原理,后來粗略看了一下AlertDialog源碼發(fā)現(xiàn)它是Dialog的子類,將View添加到WindowManager產(chǎn)生的Window對象中顯示在屏幕圖層的最上層??戳艘幌戮W(wǎng)上別人寫的源碼,感覺代碼挺簡單的,不過用起來不方便,于是封裝了一下。給大伙看一下效果。

演示一

演示二

項目地址

https://github.com/Ccapton/BottomDialog

關鍵源碼解析:

我是用AlertDialog的Builder設計模式封裝這個BottomDialog的,也方便大家熟悉這個控件。下面是關鍵的Builder類代碼

public static class Builder{
        private LinearLayout bottomLayout; //  根布局
        private View contentView;             // 內(nèi)容布局
        private Dialog dialog;             // dialog對象
        private boolean hasAnimation = true;  // 是否開啟位移動畫的標志位
        private Context context;    // activity或fragment的上下文對象
        private int layoutId;       // 內(nèi)容布局文件id

        public Builder(Context context) {
             this.context = context;
             // 從指定的布局文件中初始化根布局(不限于LinearLayout)
             bottomLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_bottomdialog,null);
        }
        public Builder setContentView(int layoutId){
            this.layoutId = layoutId;
            // 設置內(nèi)容布局,使其綁定到父布局bottomLayout,使其寬高屬性生效
            this.contentView = LayoutInflater.from(context).inflate(this.layoutId,bottomLayout);
            return Builder.this;
        }
        public Builder setContentView(View contentView){
            this.contentView = contentView;
          // 這個方法的弊端是contentView根布局寬高屬性沒有生效
            this.bottomLayout.addView(contentView);
            return Builder.this;
         }
        public Builder setHasAnimation(boolean hasAnimation){
            this.hasAnimation  = hasAnimation;
            return Builder.this;
        }

        public BottomDialog create(){

            BottomDialog bottomDialog = new BottomDialog();  // 初始化bottomDialog對象
            dialog = new Dialog(context,R.style.BottomDialog);  // 初始化dialog對象
            contentView.measure(0, 0);  // 測量contentView
            bottomLayout.measure(0,0); //  測量bottomLayout
          // 為Dialog添加View
            dialog.setContentView(bottomLayout);

            Window dialogWindow = dialog.getWindow();  // 從dialog對象中獲取window對象
// 設置Gravity,使contentView的初始位置在Window最底部,如果不設置則默認在屏幕的中央,就達不到在屏幕底部顯示的效果了
            dialogWindow.setGravity(Gravity.BOTTOM);  

            if(hasAnimation)
            dialogWindow.setWindowAnimations(R.style.DialogAnimation);

           /*
           *設置Window參數(shù)
           */
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            lp.width = (int) context.getResources().getDisplayMetrics().widthPixels;
            lp.height = bottomLayout.getMeasuredHeight();
            Log.i("BottomDialog","width = "+lp.width);
            Log.i("BottomDialog","height = "+lp.height);
            lp.alpha = 9f; // 透明度
            dialogWindow.setAttributes(lp);

          // 顯示dialog
            dialog.show();

            bottomDialog.dialog = this.dialog;
            bottomDialog.contentView = this.contentView;

            return bottomDialog;
        }
    }

這個封裝控件大致就是這樣,有興趣了解的朋友請來我的github看看
https://github.com/Ccapton/BottomDialog
https://github.com/Ccapton

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

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

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