public void showPopupWindow(View parent) {
//加载布局
layout = (LinearLayout) LayoutInflater.from(SouSuoActivity.this).inflate(
R.layout.pinpai_popwindow, null);
//找到布局的控件
// 实例化popupWindow
popupWindow = new PopupWindow(layout, 600, 700);
//控制键盘是否可以获得焦点
popupWindow.setFocusable(true);
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = SouSuoActivity.this.getWindow().getAttributes();
layoutParams.alpha = 0.5f; //0.0-1.0
//多加这一句,问题就解决了!这句的官方文档解释是:让窗口背景后面的任何东西变暗
SouSuoActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); SouSuoActivity.this.getWindow().setAttributes(layoutParams); popupWindow.setBackgroundDrawable(new BitmapDrawable(null, "")); @SuppressWarnings("deprecation") //获取xoff int xpos = manager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2; //xoff,yoff基于anchor的左下角进行偏移。 popupWindow.showAsDropDown(parent, xpos, 0); }); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // popupWindow隐藏时恢复屏幕正常透明度
setBackgroundAlpha(1.0f);
iv.setImageResource(R.mipmap.spinner_xia);
}
});
}
//此方法在activity中写入
public void setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = this.getWindow()
.getAttributes();
lp.alpha = bgAlpha;
this.getWindow().setAttributes(lp);
}
如果是fragment中使用,在依附的activity写上 setBackgroundAlpha方法,在依附的acivity写代码
public static SouSuoActivity activity;
activity=this;(加载完布局以后)
然后类调用里面的方法 SouSuoActivity.activity.setBackgroundAlpha();