popwindow is the most effective solution to pop-up screen background shadow and cancel shadow

The project requires the pop-up window to blur the background. After setting the background, it is found that clicking the outside area of ​​the pop-window will not brighten the background. The following code solves all problems.

Click on the popwindow to pop up in onclick

case 1 :
    //弹出popWiondw
    View.OnClickListener itemsOnClicks = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuWindow.dismiss();
            backgroundAlpha(1f);
        }
    };
    backgroundAlpha(0.5f);
    menuWindow = new categoryPopWiondow(getContext(),itemsOnClicks);
    menuWindow.setBackgroundDrawable(new BitmapDrawable());
    //位置
  //  menuWindow.showAtLocation(getActivity().findViewById(R.id.tv_category), Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
    menuWindow.showAtLocation(getActivity().findViewById(R.id.tv_category), Gravity.CENTER,
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    menuWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            menuWindow.dismiss();
            backgroundAlpha(1f);
        }
    });
    break;
/**
 * Set the background transparency of the added screen
 * @param bgAlpha
 */
 public void backgroundAlpha ( float bgAlpha)
{
    WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes() ;
 lp.alpha= bgAlpha ; //0.0 -1.0
 getActivity().getWindow().setAttributes(lp) ;
 getActivity( ) .getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) ;
 }              

Guess you like

Origin blog.csdn.net/qq_37870139/article/details/71514515