Android中PopupWindow位置的确定一般通过showAsDropDown函数来实现,该函数有两个重载函数,分别定义如下:
- public void showAsDropDown(View anchor) {
- showAsDropDown(anchor, 0, 0);
- }
-
- public void showAsDropDown(View anchor, int xoff, int yoff) {
- if (isShowing() || mContentView == null) {
- return;
- }
-
- registerForScrollChanged(anchor, xoff, yoff);
-
- mIsShowing = true;
- mIsDropdown = true;
-
- WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
- preparePopup(p);
-
- updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
-
- if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
- if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
-
- p.windowAnimations = computeAnimationResource();
-
- invokePopup(p);
- }
也就是说,调用第一个函数时,x和y坐标偏移量默认是0,此时PopupWindow显示的结果如下中图所示。而要实现PopupWindow显示在wenwen的正下方时,就需要程序员自己进行坐标偏移量的计算,下右图所示,当点击wenwen时,PopupWindow显示在正下方,这正是我们所需要的,对称是一种美啊。