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);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容