借助PopupWindow实现的一种通用弹窗

记录下自己实现的一个通用弹窗,使用的时候传入自己需要的view即可

public class PopWindow {
    private Activity mContext;
    private ViewGroup contentView;
    private PopupWindow popupWindow;

    public interface OnPopWindowDismissListener {
        void onPopWindowDismiss();
    }

    private OnPopWindowDismissListener onPopWindowDismissListener;

    public void setOnPopWindowDismissListener(OnPopWindowDismissListener listener) {
        this.onPopWindowDismissListener = listener;
    }

    private void createPopWindow(View contentView, int width, int height) {

        //设置弹出框的宽度和高度
        popupWindow = new PopupWindow(contentView, width, height);
//        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        //进入退出的动画
        popupWindow.setAnimationStyle(R.style.pop_window_anim_style);
    }

    public void showPopWindow(View contentView, final Activity context, int width, int height) {
        this.mContext = context;
        this.contentView = (ViewGroup) contentView;
        createPopWindow(contentView, width, height);
        popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);

        final WindowManager.LayoutParams wlBackground = context.getWindow().getAttributes();
        wlBackground.alpha = 0.5f;      // 0.0 完全不透明,1.0完全透明
        context.getWindow().setAttributes(wlBackground);
        // 当PopupWindow消失时,恢复其为原来的颜色
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                wlBackground.alpha = 1.0f;
                context.getWindow().setAttributes(wlBackground);
                if (onPopWindowDismissListener != null) {
                    onPopWindowDismissListener.onPopWindowDismiss();
                }
            }
        });
    }

    public void dismiss() {
        if (popupWindow != null)
            popupWindow.dismiss();
    }

    public boolean isShowing() {
        if (popupWindow != null)
            return popupWindow.isShowing();
        return false;
    }

}

传入的view(kotlin实现)

class SecKillEndView(var mcontext: Context) : FrameLayout(mcontext) {
    private var clickListener: OnButtonClickListener? = null

    interface OnButtonClickListener {
        fun OnButtonClick()
    }

    fun setOnButtonClickListener(listener: OnButtonClickListener) {
        clickListener = listener
    }

    init { initContentLayout() }
    private fun initContentLayout() {
        blurBgLayout(mcontext) {
            layoutParams = LayoutParams(matchParent, matchParent)
            setPageType(BlurBgLayout.PAGETYPE.OTHER_PAGE)
        }

        linearLayout {
            layoutParams = {
                val params = FrameLayout.LayoutParams(getDimension(R.dimen.w_1062px), getDimension(R.dimen.h_492px))
                orientation = LinearLayout.VERTICAL
                params
            }.invoke()

            // 标题
            textView {
                textSize = CoocaaApplication.Dpi(SkyTextSize.t_7).toFloat()
                textColor = mcontext.resources.getColor(R.color.c_3)
                text = mcontext.getString(R.string.mall_purchase_seckill_sold_out)
            }.lparams(wrapContent, wrapContent) {
                gravity = Gravity.CENTER_HORIZONTAL
                topMargin = getDimension(R.dimen.h_136px)
            }

            //描述
            textView {
                text = mcontext.getString(R.string.mall_purchase_seckill_end_restore_original_price)
                textSize = CoocaaApplication.Dpi(SkyTextSize.t_4).toFloat()
                textColor = mcontext.resources.getColor(R.color.c_3)
            }.lparams(wrapContent, wrapContent) {
                gravity = Gravity.CENTER_HORIZONTAL
                topMargin = getDimension(R.dimen.h_20px)
            }

            //按钮:我知道了
            button {
                text = mcontext.getString(R.string.mall_order_manager_detail_i_see)
                textSize = CoocaaApplication.Dpi(SkyTextSize.t_4).toFloat()
                textColor = mcontext.resources.getColor(R.color.a_5)
                backgroundDrawable = mcontext.resources.getDrawable(R.drawable.ui_sdk_btn_focus_shadow_bg)
                setOnClickListener({ clickListener?.OnButtonClick() })
            }.lparams(getDimension(R.dimen.w_464px), getDimension(R.dimen.h_233px)) {
                gravity = Gravity.CENTER_HORIZONTAL
                topMargin = CoocaaApplication.Div(-5)
            }
        }
    }
}

使用者(kotlin代码):

class SecKillEndDialog(val activity: Activity) : SecKillEndView.OnButtonClickListener {

    private var popWindow: PopWindow? = null
    private var secKillEndView: SecKillEndView? = null

    init {
        if (secKillEndView == null)
            secKillEndView = SecKillEndView(activity)
        secKillEndView!!.setOnButtonClickListener(this)
    }

     fun showDialog() {
        if (popWindow == null)
            popWindow = PopWindow()
        popWindow?.showPopWindow(secKillEndView, activity, CoocaaApplication.Div(1062), CoocaaApplication.Div(492))
    }

    override fun OnButtonClick() {
        if (popWindow != null)
            popWindow!!.dismiss()
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值