下面的PopupWindow 的高是相对于屏幕高设计,宽是获取的某一个控件的宽设置,位置位于某控件的上方,红色部分是设置弹出时屏幕变暗的。
//设置contentView
View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow_submit, null);
photoPopWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
photoPopWindow.setContentView(contentView);
//获取屏幕宽高度
DisplayMetrics dm = this.getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
//根据屏幕宽高度设置popwindow大小
photoPopWindow.setWidth((int)(upload.getWidth()));
photoPopWindow.setHeight((int)(screenHeight * 0.13)); //设置各个控件的点击响应
rl_takePhoto = contentView.findViewById(R.id.rl_takePhoto);
rl_choicePhoto = contentView.findViewById(R.id.rl_choicePhoto);
rl_takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
keep.setBackgroundColor(getResources().getColor(R.color.white));
upload.setBackgroundColor(getResources().getColor(R.color.color_success));
paste.setBackgroundColor(getResources().getColor(R.color.white));
submit();
//上传数据
photoPopWindow.dismiss();
}
});
rl_choicePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitPhoto();
//上传图片
photoPopWindow.dismiss();
}
});
int[] location = new int[];
//在控件上方显示
int[] location1 = new int[];
upload.getLocationOnScreen(location);
//photoPopWindow.showAtLocation(upload,Gravity.NO_GRAVITY, location1[0],location1[1]-photoPopWindow.getHeight());
photoPopWindow.showAtLocation(upload,Gravity.NO_GRAVITY, location1[]+upload.getWidth(),screenHeight-photoPopWindow.getHeight()-upload.getHeight()); // 设置背景颜色变暗
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.alpha = 0.7f;
this.getWindow().setAttributes(lp);
photoPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});