PopupWindow封装 (上)

本文介绍了Android中的PopupWindow,作为AlertDialog的替代品,其位置可调整。文章详细讲解了PopupWindow的基本使用,包括构造函数、设置显示View、宽高、焦点获取、触摸隐藏等,并分享了如何封装一个通用的CustomPopWindow,实现链式配置和显示,以简化代码。通过示例代码展示了创建简单PopupWindow、弹窗菜单和ListView的情况。
摘要由CSDN通过智能技术生成

PopupWindow封装 (上)

我们都知道,Android 中的弹窗基本有两种,一种是AlertDialog,另一种是PopupWindow,AlertDialog的显示位置是固定的,PopWindow 的显示位置是我们可以设置和调整的,因此,像项目中的一些场景如:某个功能的提示说明、点击按钮在按钮上方或者下方弹出菜单、新功能弹窗引导等。由于这些弹窗的位置不固定,因此都可以用PopupWindow来做。最近项目中也用到了PopupWindow弹窗功能,在写的过程中,发现虽然API比较简单,但是写一个PopupWindow,还是有点繁琐,很多重复代码,决定简单封装一下,以后添加一个弹窗就不用那么麻烦了,几行代码就搞定了。本篇文章是对项目中使用 PopupWindow做一个笔记和总结。

1.1 首先先看一下效果图

这里写图片描述

以上示例展示了4中PopupWindow 使用场景

Github地址:CustomPopWindow

1.2 PopupWindow 简单介绍

首先看一下Google 官方文档对PopupWindow的介绍:

This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

大致意思就是:popup window 是一个出现在当前Activity顶层的悬浮容器,可以用来展示任意的View。

因此只要是个View,都可以用PopupWindow 来展示。这里我就不过多介绍PopupWindow 的API,挑几个重要并且常用的说一下:

1.2.1 构造函数,这个不用多说,多个重载函数,穿不同的参数。

public PopupWindow(int width, int height)

public PopupWindow(View contentView, int width, int height) 

public PopupWindow(View contentView, int width, int height, boolean focusable) 

1.2.2 设置显示的View

public void setContentView(View contentView)

1.2.3 设置展示的宽、高,构造函数传了宽高就不用重新设置

// 设置宽,其实构造函数也是调用的这个方法
public void setWidth(int width)
//设置高
public void setHeight(int height)

1.2.4 设置是否获取焦点

public void setFocusable(boolean focusable)

1.2.5 设置点击PopupWindow 以外区域是否可以隐藏PopupWindow

public void setOutsideTouchable(boolean touchable)

注意:这里要注意一下,有时侯我们希望触摸PopupWindow 以外区域就隐藏PopupWindow,理论上我们只需要调用 setOutsideTouchable(ture)设置为ture就可以了,但是实际上只设置这个属性是不行的,必须设置背景,也就是说要和setBackgroundDrawable(Drawable background)同时使用才有效,不然,点击PopupWindow以外区域是不能隐藏掉的。

1.2.6 隐藏PopupWindow

public void dismiss()

1.2.7 设置dissmiss 回调监听

public void setOnDismissListener(OnDismissListener onDismissListener)

1.2.8 显示PopupWindow

//直接显示在参照View 的左下方
public void showAsDropDown(View anchor)
// 显示在参照View的左下方,可以通过xoff,yOff,来调节x,y方向的偏移
public void showAsDropDown(View anchor, int xoff, int off)

public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
//显示在指定位置,相对于整个屏幕的window而言,通过gravity调解显示在左、上、右、下、中. x,y调整两个方向的偏移
public void showAtLocation(View parent, int gravity, int x, int y) 

以上就是PopupWindow 重要的并且常用的API。

1.3 封装通用PopupWindow,CustomPopWindow,使用链式的方式配置并显示

由于每次写PopupWindow都要写很多重复代码,因此简单的封装了一个CustomPopWindow.封装了PopupWindow 的一些常用API,使用Builder模式,就像写AlertDialog 一样,链式配置。

使用方法:

1.3.1 简便写法:

CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(this)
    .setView(R.layout.pop_layout1)//显示的布局,还可以通过设置一个View
    //     .size(600,400) //设置显示的大小,不设置就默认包裹内容
    .setFocusable(true)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值