final View contentView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_changereason, null); PopupWindow mPopWindow = new PopupWindow(contentView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true); mPopWindow.setContentView(contentView); mPopWindow.setFocusable(true); mPopWindow.setOutsideTouchable(false); darkenBackground(0.2f);//设置popupwindow弹出的时候背景是灰色 contentView.findViewById(R.id.xxxx); //需要在popupwindow消失的时候把背景变回白色 mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { darkenBackground(1f); } });
View rootview = LayoutInflater.from(mContext).inflate(R.layout.activity_xxxx, null); mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
//这是改变popupwindow弹出时背景变灰 private void darkenBackground(Float bgcolor) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = bgcolor; getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().setAttributes(lp); }