加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Android中PopupWindow自定义坐标达成

发布时间:2021-11-23 15:32:59 所属栏目:教程 来源:互联网
导读:Android中PopupWindow位置的确定一般通过showAsDropDown函数来实现,该函数有两个重载函数,分别定义如下: public void showAsDropDown(View anchor) { showAsDropDown(anchor, 0, 0); } public void showAsDropDown(View anchor, int xoff, int yoff) { if
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显示在正下方,这正是我们所需要的,对称是一种美啊。
 
 

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读