在activity中
private PopupWindow popupWindow;
public void setComparePop(PopupWindow pop){
this.popupWindow = pop;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(popupWindow != null && popupWindow.isShowing()){
return false;
}
return super.dispatchTouchEvent(ev);
}
在fragment中
case R.id.fragment5_tcdl:
//弹出选择对比列表的框
if (popupWindow != null) {
popupWindow = null;
}
backgroundAlpha(0.6f);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.popupwindow_layout, null);
Button popupwind_qvxiao = (Button) customView.findViewById(R.id.popupwind_qvxiao);//取消
Button popupwind_queding = (Button) customView.findViewById(R.id.popupwind_queding);//确定
TextView popupwind_text = (TextView) customView.findViewById(R.id.popupwind_text);//内容
popupwind_text.setText("是否退出登录");
popupWindow = new PopupWindow(customView,600, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(relative1, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(true);
((ShowActivity)getActivity()).setComparePop(popupWindow);
//取消
popupwind_qvxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backgroundAlpha(1f);
if (popupWindow!= null) {
popupWindow.dismiss();
setclick();
}
}
});
//确定
popupwind_queding.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
break;
添加屏幕透明度
/**
* 设置添加屏幕的背景透明度
* @param bgAlpha 外部透明度
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getActivity().getWindow().setAttributes(lp);
}