歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中PopupWindow自定義坐標實現

Android中PopupWindow自定義坐標實現

日期:2017/3/1 10:20:52   编辑:Linux編程

Android中PopupWindow位置的確定一般通過showAsDropDown函數來實現,該函數有兩個重載函數,分別定義如下:

  1. public void showAsDropDown(View anchor) {
  2. showAsDropDown(anchor, 0, 0);
  3. }
  4. public void showAsDropDown(View anchor, int xoff, int yoff) {
  5. if (isShowing() || mContentView == null) {
  6. return;
  7. }
  8. registerForScrollChanged(anchor, xoff, yoff);
  9. mIsShowing = true;
  10. mIsDropdown = true;
  11. WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
  12. preparePopup(p);
  13. updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
  14. if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
  15. if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
  16. p.windowAnimations = computeAnimationResource();
  17. invokePopup(p);
  18. }

也就是說,調用第一個函數時,x和y坐標偏移量默認是0,此時PopupWindow顯示的結果如下中圖所示。而要實現PopupWindow顯示在wenwen的正下方時,就需要程序員自己進行坐標偏移量的計算,下右圖所示,當點擊wenwen時,PopupWindow顯示在正下方,這正是我們所需要的,對稱是一種美啊。

Copyright © Linux教程網 All Rights Reserved