引入

  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 ofmyhub@gmail.com

  * @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();

  }

  }

  }


更多相关文章

  1. GestureDetector部分源码分析
  2. Android中ConvenientBanner的使用
  3. Android中Dialog设置外部点击事件
  4. android 拍照图片旋转问题
  5. Android中XML文件解析
  6. Widget的点击事件
  7. android MotionEvent详解
  8. android viewpage的施用
  9. Android(安卓)GestureDetector滑动监听事件

随机推荐

  1. Android(安卓)之 Window、WindowManager
  2. android自定义adapter 滑动屏幕时 进度条
  3. Android中XML绘图:Bitmap/Shape/Seletor和
  4. Android(安卓)安全
  5. 导入的ANDROID 项目没有ANDROID的JAR包
  6. Android之自定义五子棋View
  7. Android画板
  8. 我的Android之旅——学习、项目、心态
  9. Android工程 引用另外一个Android工程
  10. Android内核开发:序