PopupWindow 弹出时背景变暗

 

 

下面的PopupWindow  的高是相对于屏幕高设计,宽是获取的某一个控件的宽设置,位置位于某控件的上方,红色部分是设置弹出时屏幕变暗的。

 

 1      //设置contentView
 2         View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow_submit, null);
 3         photoPopWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
 4         photoPopWindow.setContentView(contentView);
 5         //获取屏幕宽高度
 6         DisplayMetrics dm = this.getResources().getDisplayMetrics();
 7         int screenWidth = dm.widthPixels;
 8         int screenHeight = dm.heightPixels;
 9         //根据屏幕宽高度设置popwindow大小
10         photoPopWindow.setWidth((int)(upload.getWidth()));
11         photoPopWindow.setHeight((int)(screenHeight * 0.13));
12 
13         //设置各个控件的点击响应
14         rl_takePhoto = contentView.findViewById(R.id.rl_takePhoto);
15         rl_choicePhoto = contentView.findViewById(R.id.rl_choicePhoto);
16         rl_takePhoto.setOnClickListener(new View.OnClickListener() {
17             @Override
18             public void onClick(View v) {
19                 keep.setBackgroundColor(getResources().getColor(R.color.white));
20                 upload.setBackgroundColor(getResources().getColor(R.color.color_success));
21                 paste.setBackgroundColor(getResources().getColor(R.color.white));
22                 submit();
23                 //上传数据
24                 photoPopWindow.dismiss();
25             }
26         });
27         rl_choicePhoto.setOnClickListener(new View.OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                 submitPhoto();
31                 //上传图片
32                 photoPopWindow.dismiss();
33             }
34         });
35         int[] location = new int[2];
36         //在控件上方显示
37         int[] location1 = new int[2];
38         upload.getLocationOnScreen(location);
39         //photoPopWindow.showAtLocation(upload,Gravity.NO_GRAVITY, location1[0],location1[1]-photoPopWindow.getHeight());
40         photoPopWindow.showAtLocation(upload,Gravity.NO_GRAVITY, location1[0]+upload.getWidth(),screenHeight-photoPopWindow.getHeight()-upload.getHeight());
41 
42         // 设置背景颜色变暗
43         WindowManager.LayoutParams lp = this.getWindow().getAttributes();
44         lp.alpha = 0.7f;
45         this.getWindow().setAttributes(lp);
46         photoPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
47 
48             @Override
49             public void onDismiss() {
50                 WindowManager.LayoutParams lp = getWindow().getAttributes();
51                 lp.alpha = 1f;
52                 getWindow().setAttributes(lp);
53             }
54         });

 

posted @ 2018-07-30 17:29  vetter  阅读(2600)  评论(0编辑  收藏  举报