private void showPopUp(View v) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.WHITE);
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200));
tv.setText("xxxxx");
tv.setTextColor(Color.RED);
layout.addView(tv);
//通过改变当前activity的window属性来控制
WindowManager.LayoutParams lp = getWindow()
.getAttributes();
lp.alpha = 0.4f;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
PopupWindow popupWindow = new PopupWindow(layout, 120, 120);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.setBackgroundDrawable(null);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] + popupWindow.getHeight() + 20);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//此时记得也调用一下哦
WindowManager.LayoutParams lp = getWindow()
.getAttributes();
lp.alpha = 1f;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
}
});
popupWindow.update();
}
网友评论