Android PopupWindow怎麼合理控制彈出位置(showAtLocation)

來源:互聯網
上載者:User

標籤:

說到PopupWindow,應該都會有種熟悉的感覺,使用起來也很簡單

// 一個自訂的布局,作為顯示的內容Context context = null;  // 真實環境中要賦值int layoutId = 0;      // 布局IDView contentView = LayoutInflater.from(context).inflate(layoutId, null);      final PopupWindow popupWindow = new PopupWindow(contentView,                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);popupWindow.setTouchable(true);// 如果不設定PopupWindow的背景,有些版本就會出現一個問題:無論是點擊外部地區還是Back鍵都無法dismiss彈框// 這裡單獨寫一篇文章來分析popupWindow.setBackgroundDrawable(new ColorDrawable());// 設定好參數之後再showpopupWindow.showAsDropDown(contentView);

  

這種showAsDropDown預設只會向下彈出顯示,這種情況有個最明顯的缺點就是:彈視窗可能被螢幕截斷,顯示不全所以需要使用到另外一個方法showAtLocation,這個的座標是相對於整個螢幕的,所以需要我們自己計算位置。如所示,我們可以根據螢幕左上方的座標,螢幕高寬,點擊View的左上方的座標C,點擊View的大小以及PopupWindow布局的大小計算出PopupWindow的顯示位置B

 

計算方法源碼如下:

    /**     * 計算出來的位置,y方向就在anchorView的上面和下面對齊顯示,x方向就是與螢幕右邊對齊顯示     * 如果anchorView的位置有變化,就可以適當自己額外加入位移來修正     * @param anchorView  呼出window的view     * @param contentView   window的內容布局     * @return window顯示的左上方的xOff,yOff座標     */    private static int[] calculatePopWindowPos(final View anchorView, final View contentView) {        final int windowPos[] = new int[2];        final int anchorLoc[] = new int[2];     // 擷取錨點View在螢幕上的左上方座標位置        anchorView.getLocationOnScreen(anchorLoc);        final int anchorHeight = anchorView.getHeight();        // 擷取螢幕的高寬        final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());        final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);        // 計算contentView的高寬        final int windowHeight = contentView.getMeasuredHeight();        final int windowWidth = contentView.getMeasuredWidth();        // 判斷需要向上彈出還是向下彈出顯示        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);        if (isNeedShowUp) {            windowPos[0] = screenWidth - windowWidth;            windowPos[1] = anchorLoc[1] - windowHeight;        } else {            windowPos[0] = screenWidth - windowWidth;            windowPos[1] = anchorLoc[1] + anchorHeight;        }        return windowPos;    }

 

  接下來調用showAtLoaction顯示:

int windowPos[] = calculatePopWindowPos(view, windowContentViewRoot);int xOff = 20;windowPos[0] -= xOff;popupwindow.showAtLocation(view, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
// windowContentViewRoot是根布局View

上面的例子只是提供了一種計算方式,在實際開發中可以根據需求自己計算,比如anchorView在左邊的情況,在中間的情況,可以根據實際需求寫一個彈出位置能夠自適應的PopupWindow。

 

補充上擷取螢幕高寬的代碼ScreenUtils.java:

    /**     * 擷取螢幕高度(px)     */    public static int getScreenHeight(Context context) {        return context.getResources().getDisplayMetrics().heightPixels;    }    /**     * 擷取螢幕寬度(px)     */    public static int getScreenWidth(Context context) {        return context.getResources().getDisplayMetrics().widthPixels;    }

 

Android PopupWindow怎麼合理控制彈出位置(showAtLocation)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.