Android 安卓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() {

popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结尾

  • 腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升

img

  • 一线互联网Android面试题含详解(初级到高级专题)

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

img

有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

[外链图片转存中…(img-Otb1AoGk-1712351625184)]

有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码: 首先,在你的布局文件中定义一个按钮,点击该按钮将会弹出提示框: ```xml <Button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除冗余文件" android:onClick="showPopup"/> ``` 接下来,在你的Activity中实现showPopup()方法: ```java public void showPopup(View view) { // 创建一个PopupWindow对象 PopupWindow popupWindow = new PopupWindow(this); // 设置PopupWindow的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(this).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 设置PopupWindow的焦点,使其能够响应点击事件 popupWindow.setFocusable(true); // 设置PopupWindow的背景,这样点击PopupWindow外部的空白区域,就能够自动关闭PopupWindow popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置PopupWindow的动画 popupWindow.setAnimationStyle(R.style.PopupAnimation); // 显示PopupWindow popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); } ``` 在上面的代码中,我们首先创建了一个PopupWindow对象,然后设置了它的宽度和高度,并且将布局文件popup_layout.xml设置为其内容视图。接着,我们设置了PopupWindow的焦点和背景,使其能够响应点击事件,并且在点击空白区域时自动关闭。最后,我们设置了PopupWindow的动画,并且调用showAtLocation()方法将其显示在屏幕的中央位置。 最后,你需要创建popup_layout.xml布局文件,这个布局文件可以自定义,用来展示你要提示用户的信息。例如,以下是一个简单的示例布局文件: ```xml <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="#FFFFFF" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否删除冗余文件?" android:textSize="18sp" android:textColor="#000000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="16dp"> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:textColor="#000000" android:background="#EEEEEE" android:layout_weight="1"/> <Button android:id="@+id/btn_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:textColor="#FFFFFF" android:background="#FF4081" android:layout_weight="1"/> </LinearLayout> </LinearLayout> ``` 在这个布局文件中,我们使用了一个垂直方向的LinearLayout来包含一个TextView和一个水平方向的LinearLayout,水平方向的LinearLayout包含了“取消”和“确定”两个Button按钮。 现在你就可以通过点击“删除冗余文件”按钮,弹出提示框,询问用户是否进行冗余文件的删除了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值