一 显示函数:
showAsDropDown(View anchor)
相对于某个控件无平移
showAsDropDown(View anchor, int xoff, int yoff)
相对与某个控件的位置有偏移,
Xoff:正值表示向左,负值右
yoff:正值表示向下,负值上
showAsDropDown(View anchor, int xoff, int yoff, int gravity)
api:19引入的
多了相对于anchor控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等)
showAtLocation(View parent, int gravity, int x, int y)
相对于父控件的位置,可偏移,也可不偏移
二 背景
PopupWindow重写以下几个方法
private ViewGroup mView;
@Override
public void showAsDropDown(View anchor) {
showAsDropDown(anchor, 0, 0);
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
super.showAsDropDown(anchor, xoff, yoff);
mView = (ViewGroup) anchor.getRootView();
setShowBackground(anchor);
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
super.showAsDropDown(anchor, xoff, yoff, gravity);
mView = (ViewGroup) anchor.getRootView();
setShowBackground(anchor);
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
super.showAtLocation(parent, gravity, x, y);
mView = (ViewGroup) parent.getRootView();
setShowBackground(parent);
}
protected void setShowBackground(View view) {
mView.addView(mBackground);
AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
alpha.setDuration(500);
mBackground.setAnimation(alpha);
}
@Override
public void dismiss() {
//关闭移除背景
mView.removeView(mBackground);
super.dismiss();
}