Android 安卓PopupWindow工具类

文章介绍了如何在Android应用中使用PopupWindow根据View事件的位置动态调整显示位置,并提供了PopUtils工具类的封装,同时提到了面试题的相关资源。
摘要由CSDN通过智能技术生成

效果图


在这里插入图片描述

示例使用方法


分别有不同的效果:

在按钮上方显示

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() {

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);

总结

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021最新面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。

2020面试真题解析
腾讯面试真题解析

阿里巴巴面试真题解析

字节跳动面试真题解析
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
4413892340)]

[外链图片转存中…(img-Kp51BJYj-1714413892342)]

[外链图片转存中…(img-zARHX2tt-1714413892343)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值