如果要将PopupWindow居中时,直接:
popupWindow.showAtLocation(findViewById(R.id.btnBack), Gravity.CENTER, 0, 0);//设置居中,经测试,第一个参数可以不是整个视图,只要是视图中的一个按钮都可以的了;
我试过在PopupWindow的view布局中用RelativeLayout将view设置layout_centerInParent="true",layout_height已经设置成wrap_content,但效果看到的layout_height高度还是match_parent,经过排查,就是layout_centerInParent="true"导致的,这不科学嘛!因为我已经在其它都用了layout_height="wrap_content",但高度还是占据整个屏幕,这个真心不懂。。。。
PopupWindow点击消失
1.设置背景
不需要颜色:popupWindow.setBackgroundDrawable(new BitmapDrawable());
自定义颜色:
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable bg = new ColorDrawable(0xb0000000);
// 设置PopupWindow弹出窗体的背景
popupWindow.setBackgroundDrawable(bg);
2.popupWindow.setOutsideTouchable(true);
PopupWindow弹出后设置背景透明度
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.3f;
getWindow().setAttributes(lp);
在消失的时候将透明度变回来
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});