标签:
1、创建一个PopupWindow
/** * 创建一个PopupWindow */ private void createPopupWindow() { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.popup_window_layout, null); popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 产生activity背景变暗效果 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp); popupWindow.setFocusable(true);// PopUpWindow可以看作一个类似于模态对话框的东西,如果设置是false只是一个浮现在当前界面上的view而已,不影响当前界面的任何操作 popupWindow.setOutsideTouchable(true); // 点击popWin以处的区域,自动关闭 ColorDrawable cd = new ColorDrawable(0x000000); popupWindow.setBackgroundDrawable(cd); // 如果不设置PopupWindow的背景,点击外部区域无法dismiss弹框,不能触发OnDismissListener popupWindow.setAnimationStyle(R.style.PopupWindowAnimation); popupWindow.showAsDropDown(titleBarView, titleBarView.getWidth() - popupWindow.getWidth(), 0);// 设置好参数之后再show popupWindow.update(); // 刷新状态 // 监听PopupWindow被dismiss的时候,恢复activity透明度 popupWindow.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } }); }
标签:
原文地址:http://www.cnblogs.com/fuyanan/p/4667766.html