广告
返回顶部
首页 > 资讯 > 移动开发 >Android 安卓PopupWindow工具类
  • 918
分享到

Android 安卓PopupWindow工具类

popupwindowAndroid 2022-06-06 13:06:00 918人浏览 泡泡鱼
摘要

文章目录引入效果图示例使用方法相关解释工具类封装最后奉上汪姑娘的Blog 引入 Android在需要类似于这样的弹窗会用到PopupWindo

文章目录引入效果图示例使用方法相关解释工具类封装最后奉上汪姑娘的Blog 引入

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事件位置弹出

public class PopUtils {
    private View v;
    private PopupWindow popupWindow;
    private int[] location;
    private int popupWidth;
    private int popupHeight;
    
    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);
    }
    
    public void showPop() {
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }
    
    public void showPopY(int offsetY) {
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - offsetY);
    }
    
    public void showPopX(int offsetX) {
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2 - offsetX, location[1] - popupHeight);
    }
    
    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);
    }
    
    public void dismissPop() {
        if (null != popupWindow) {
            popupWindow.dismiss();
        }
    }
}
最后奉上汪姑娘的Blog

https://blog.csdn.net/qq_35456045
Https://blog.csdn.net/qq_35456045
https://blog.csdn.net/qq_35456045


作者:程忆难


--结束END--

本文标题: Android 安卓PopupWindow工具类

本文链接: https://www.lsjlt.com/news/29186.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作