这篇文章主要介绍了popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
在控件的其他方向上显示只需修改最后一行代码即可,如:
下方:popupWindow.showAsDropDown(v);
左边:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右边:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
// 相对某个控件的位置(正左下方),无偏移
pop.showAsDropDown(View anchor)
// 相对某个控件的位置,有偏移,xoff 为 X 轴的偏移量,yoff 为 Y 轴的偏移量
pop.showAsDropDown(View anchor, int xoff, int yoff)
// 在父容器的什么位置,gravity 为相对位置,如:正中央 Gravity.CENTER、下方 Gravity.BOTTOM、Gravity.RIGHT|Gravity.BOTTOM 右下方等,后面两个参数为 x/y 轴的偏移量。
pop.showAtLocation(View parent, int gravity, int x, int y)
关闭 PopupWindow
pop.dismiss()