Android PopupWindow背景半透明兼容方案

View contentView;
   LayoutInflater mLayoutInflater = LayoutInflater.from(activity);
   contentView = mLayoutInflater.inflate(R.layout.layout_popupwindow,
           null);
   pop = new PopupWindow(contentView,
           ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.y568));
   TextView tvTitle = (TextView) contentView.findViewById(R.id.text);
   tvTitle.setText(strTitle);
   ListView listView = (ListView) contentView.findViewById(R.id.list);
   // 产生背景变暗效果
   WindowManager.LayoutParams lp = activity.getWindow()
           .getAttributes();
   lp.alpha = 0.4f;
   activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
   activity.getWindow().setAttributes(lp);
   pop.setTouchable(true);
   pop.setFocusable(true);
   pop.setBackgroundDrawable(new BitmapDrawable());
   pop.setOutsideTouchable(true);
   pop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
   pop.update();
   pop.setOnDismissListener(new PopupWindow.OnDismissListener() {

       // 在dismiss中恢复透明度
       public void onDismiss() {
           WindowManager.LayoutParams lp = activity.getWindow()
                   .getAttributes();
           lp.alpha = 1f;
           activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
           activity.getWindow().setAttributes(lp);
       }
   });
   listView.setOnItemClickListener(onItemClickListener);
   listView.setAdapter(adapter);

你可能感兴趣的:(Android PopupWindow背景半透明兼容方案)