最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation)

 更新時間:2017年10月30日 16:42:30   作者:popfisher  
本篇文章主要介紹了詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation),具有一定的參考價值,有興趣的可以了解一下

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

// 一個自定義的布局,作為顯示的內(nèi)容
Context context = null;  // 真實環(huán)境中要賦值
int layoutId = 0;      // 布局ID
View contentView = LayoutInflater.from(context).inflate(layoutId, null);
   
final PopupWindow popupWindow = new PopupWindow(contentView,
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

popupWindow.setTouchable(true);
// 如果不設置PopupWindow的背景,有些版本就會出現(xiàn)一個問題:無論是點擊外部區(qū)域還是Back鍵都無法dismiss彈框
// 這里單獨寫一篇文章來分析
popupWindow.setBackgroundDrawable(new ColorDrawable());
// 設置好參數(shù)之后再show
popupWindow.showAsDropDown(contentView);

如果創(chuàng)建PopupWindow的時候沒有指定高寬,那么showAsDropDown默認只會向下彈出顯示,這種情況有個最明顯的缺點就是:彈窗口可能被屏幕截斷,顯示不全,所以需要使用到另外一個方法showAtLocation,這個的坐標是相對于整個屏幕的,所以需要我們自己計算位置。

如下圖所示,我們可以根據(jù)屏幕左上角的坐標A,屏幕高寬,點擊View的左上角的坐標C,點擊View的大小以及PopupWindow布局的大小計算出PopupWindow的顯示位置B

 

計算方法源碼如下:

  /**
   * 計算出來的位置,y方向就在anchorView的上面和下面對齊顯示,x方向就是與屏幕右邊對齊顯示
   * 如果anchorView的位置有變化,就可以適當自己額外加入偏移來修正
   * @param anchorView 呼出window的view
   * @param contentView  window的內(nèi)容布局
   * @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顯示:

View windowContentViewRoot = 我們要設置給PopupWindow進行顯示的View
int windowPos[] = calculatePopWindowPos(view, windowContentViewRoot);
int xOff = 20;// 可以自己調整偏移
windowPos[0] -= xOff;
popupwindow.showAtLocation(view, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
// windowContentViewRoot是根布局View

上面的例子只是提供了一種計算方式,在實際開發(fā)中可以根據(jù)需求自己計算,比如anchorView在左邊的情況,在中間的情況,可以根據(jù)實際需求寫一個彈出位置能夠自適應的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;
  }

Demo截圖展示:

Demo下載地址:https://github.com/PopFisher/SmartPopupWindow

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

汕尾市| 鄂温| 莱阳市| 胶州市| 喀喇沁旗| 冕宁县| 嘉兴市| 灵璧县| 和龙市| 怀柔区| 开原市| 昌宁县| 遂溪县| 嘉兴市| 华阴市| 新津县| 漳平市| 措美县| 霍林郭勒市| 阿勒泰市| 缙云县| 盐亭县| 泊头市| 临清市| 晋中市| 句容市| 无为县| 奇台县| 平罗县| 墨竹工卡县| 苍山县| 三明市| 增城市| 本溪| 沧州市| 连云港市| 淳安县| 鄂伦春自治旗| 阜康市| 边坝县| 拜城县|