开源库【CustomPopupWindow】---想怎么弹就怎么弹

最近对PopupWindow封装了一个库,先看一下demo演示:

这里写图片描述

效果说明

  • 提供了六种不同的弹出方式,后面会有详细说明

  • 默认给弹出view加上了灰色背景,but背景位置不支持更改。

  • 弹出view默认在灰色背景中的正中间,也可以自己设置,并可微调

  • 可设置弹出和消失style,如动画效果

  • 点击灰色背景外和背景中非view区弹窗都会消失

技术分析

  • 将传入的view作为子view放入一个灰色的父view(FrameLayout)中
frameLayout.removeAllViews();
frameLayout.addView(contentView, new ViewGroup.LayoutParams(width, height));
  • 获取目标view的位置,结合想实现的效果,就可以确定父view(FrameLayout)的位置
int[] location = new int[2];
parentView.getLocationOnScreen(location);
  • 确定传入的view在父view中的位置:
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) contentView.getLayoutParams();
tryClearGravity();
hasSetGravity = false;
lp.gravity = gravity;
if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
     lp.rightMargin = -xOff;
} else {
     lp.leftMargin = xOff;
}
if ((gravity & Gravity.BOTTOM) == Gravity.BOTTOM) {
     lp.bottomMargin = -yOff;
} else {
    lp.topMargin = yOff;
}
contentView.setLayoutParams(lp);
  • 最后用PopupWindowshowAsDropDownshowAtLocation方法显示弹窗。
popupWindow.showAsDropDown(parentView, 0, 0);

或者

popupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight());

库使用方法

1. 在项目的build.gradle文件最后添加:
allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
2.modulebuild.gradle文件中添加:
 dependencies {
    compile 'com.github.Sbingo:CustomPopupWindow:v1.0.0'
 }

####3. 开始使用

  • 新建CustomPopupWindow对象

customPopupWindow = new CustomPopupWindow(PopupWindowActivity.this);

  • 创建要弹出的view

popupTestView = new PopupTestView(PopupWindowActivity.this);

  • 弹出view(共有6种方式,这里只写一种作为示例)
customPopupWindow.showOnBottom(v, popupTestView, 0, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0, 100);
可选配置
  • 设置弹窗消失的监听器
customPopupWindow.setDismissListener(new CustomPopupWindow.DismissCallBack() {
      @Override
      public void dismissCallBack() {
            /** do sth you like here */
       }
});
  • 设置弹出view在灰色背景中的位置(默认在正中间)
customPopupWindow.setGravity(Gravity.BOTTOM | Gravity.RIGHT);

可以同时结合各方法中的偏移进行微调。

  • 设置弹出和消失style

    对应于每个show()方法中的style参数,详见下面的说明
    ###方法参数说明
    showOnBottom(View parentView, View contentView, int style, int width, int height, int xOff, int yOff)方法为例,它的作用是在目标view下方弹出view,参数说明:

  • parentView

用于确定弹出位置或(和)获取windowToken的view,最好传入目标view。

  • contentView

弹出的view,在演示中为灰色背景中的白色区域。

  • style

弹出和消失时的效果,-1时默认效果,0时无效果,也可传入style资源id,可参照demo。

  • width

弹出view的宽度,可以是
ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT
或者固定像素大小

  • height

弹出view的高度,可以是
ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT
或者固定像素大小

  • xOff

弹出view在灰色背景中的水平偏移

  • yOff

弹出view在灰色背景中的竖直偏移

其余还有5个方法,分别是:
showOnTopshowOnLeftshowOnRightshowOnScreenBottomshowFullScreen
相信看了名字就知道它们的作用,各参数的意义也和上面的说明一样。

源码和demo在这里

如果觉得有用,不妨顺手点个Star,谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值