借助PopupWindow实现的一种通用弹窗
来源:互联网 发布:电力施工软件 编辑:程序博客网 时间:2024/06/05 08:38
记录下自己实现的一个通用弹窗,使用的时候传入自己需要的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
- 借助PopupWindow实现的一种通用弹窗
- popupWindow 的一种实现写法
- 打造通用的PopupWindow
- 使用PopupWindow实现的弹窗
- PopupWindow实现弹窗效果
- 又一种JSP实现的Java通用简单分页算法
- 实现分页的通用存储过程(第一种)
- 导出excel功能,较通用的一种实现
- 一种TV(Android系统)通用焦点框的实现
- Android中PopupWindow实现弹窗输入的效果
- Android开发技术学习之popupwindow的弹窗实现
- Android开发技术学习之popupwindow的弹窗实现
- Android 封装一个通用的PopupWindow
- 如何封装一个通用的PopupWindow
- Android 开发之通用的 PopupWindow
- Android 封装一个通用的PopupWindow
- 拒绝无用功,封装一个通用的PopupWindow
- 拒绝无用功,封装一个通用的 PopupWindow
- Breadth-first Search -- Leetcode problem513. Find Bottom Left Tree Value
- 在一个数组中找两个数,使得它们的和为一个指定的数
- 对象数组中,将对象中,相同的key-value对象,分类出来
- erlang要点
- Golang
- 借助PopupWindow实现的一种通用弹窗
- JavaScript getter setter的使用
- 一道难题Tree
- mui相关资料链接
- IntelliJ Idea 连接 SVN
- linux下查询端口,进程的状态以及netstat的参数意义
- Maven 的配置
- Map集合遍历的五种方式
- 将mongodb添加到Windows服务