Android安卓 PopupWindow工具類

  引入

  Android在需要類似於這樣的彈窗會用到PopupWindow,所以,我爲此封裝了PopupWindow工具類,

  示例使用方法

  分別有不同的效果:

  在按鈕上方顯示

  View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);

  PopUtils popUtils1 = new PopUtils(v, inflate1);

  popUtils1.showPop();

  在按鈕向上偏移100顯示,向下改爲負數即可

  View inflate2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);

  PopUtils popUtils2 = new PopUtils(v, inflate2);

  popUtils2.showPopY(100);

  在按鈕向左偏移100顯示,向右改爲負數即可

  View inflate3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);

  PopUtils popUtils3 = new PopUtils(v, inflate3);

  popUtils3.showPopX(100);

  在按鈕向上偏移100顯示,向左偏移100顯示,相反則改爲負數即可

  View inflate4 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);

  PopUtils popUtils4 = new PopUtils(v, inflate4);

  popUtils4.showPopXY(100, 100);

  相關解釋

  //獲取View,R.layout.xxx 是PopupWindow要展示的內容

  View inflate = LayoutInflater.from(上下文Context).inflate(R.layout.xxx, null, false);

  //在這裏寫PopupWindow中的控件相關事件

  ...

  //實例化

  //v:點擊/長按事件傳回來的View

  //inflate:PopupWindow要展示的View

  PopUtils popUtils = new PopUtils(v, inflate);

  //顯示PopupWindow

  popUtils.showPop();

  工具類封裝

  綜合幾點考慮而封裝

  定義PopupWindow窗口位置

  根據View事件位置彈出

  /**

  * @author ThirdGoddess

  * @email [email protected]

  * @Github https://github.com/ThirdGoddess

  * @date :2020-03-17 01:42

  */

  public class PopUtils {

  private View v;

  private PopupWindow popupWindow;

  private int[] location;

  private int popupWidth;

  private int popupHeight;

  /**

  * @param v 事件View

  * @param inflate 彈框item

  */

  PopUtils(View v, View inflate) {

  this.v = v;

  popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

  popupWindow.setBackgroundDrawable(new BitmapDrawable());

  inflate.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

  popupWidth = inflate.getMeasuredWidth();

  popupHeight = inflate.getMeasuredHeight();

  location = new int[2];

  v.getLocationOnScreen(location);

  }

  /**

  * PopupWindow出現在事件View上方

  */

  public void showPop() {鄭州婦科醫院 http://www.0371zzkd.com/

  popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);

  }

  /**

  * 偏移y軸

  *

  * @param offsetY Y軸偏移(正數上移,負數下移)

  */

  public void showPopY(int offsetY) {

  popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - offsetY);

  }

  /**

  * 偏移x軸

  *

  * @param offsetX X軸偏移(正數左移,負數右移)

  */

  public void showPopX(int offsetX) {

  popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2 - offsetX, location[1] - popupHeight);

  }

  /**

  * 偏移x軸和y軸

  *

  * @param offsetX X軸偏移(正數左移,負數右移)

  * @param offsetY Y軸偏移(正數上移,負數下移)

  */

  public void showPopXY(int offsetX, int offsetY) {

  popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2 - offsetX, location[1] - popupHeight - offsetY);

  }

  /**

  * 關閉PopupWindow

  */

  public void dismissPop() {

  if (null != popupWindow) {

  popupWindow.dismiss();

  }

  }

  }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章