private void showMom() { if (pop.isShowing()) { pop.dismiss(); } else { //位于控件下面 pop.showAsDropDown(view, ScreenUtils.getScreenWidth(context), 100); //titleBar 正下方 //屏幕位置 // pop.showAtLocation(view, Gravity.BOTTOM, 0, 0); //实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0x00000000); //设置SelectPicPopupWindow弹出窗体的背景 pop.setBackgroundDrawable(dw); // CommUtil.backgroundAlpha(ProjectionSonActivity.this, 0.5f);//0.0-1.0 // pop.setOnDismissListener(new PopupWindow.OnDismissListener() { // @Override // public void onDismiss() { // // TODO Auto-generated method stub // CommUtil.backgroundAlpha(ProjectionSonActivity.this, 1f); // } // }); } } private View view; private PopupWindow pop; //弹出滚动条 private void initPopupWindow() { view = this.getLayoutInflater().inflate(R.layout.projec_popuw, null); pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); //获取屏幕的宽度 int w = this.getWindowManager().getDefaultDisplay().getWidth(); // pop.setWidth(w - 40); pop.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件 //实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0x00000000); pop.setBackgroundDrawable(dw);// 设置背景图片,不能在布局中设置,要通过代码来设置 pop.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功 }
/** * 设置添加屏幕的背景透明度 * * @param bgAlpha */ public static void backgroundAlpha(Activity context, float bgAlpha) { WindowManager.LayoutParams lp = context.getWindow().getAttributes(); lp.alpha = bgAlpha; context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); context.getWindow().setAttributes(lp); }