public abstract class BasePopupWindow extends Object implements PopupWindow.OnDismissListener
这是一个快速实现PopupWindow的基类,本基类易于扩展,并且几乎没有使用限制,便于您快速实现各种各样的PopupWindow。
BasePopup已经为您内部实现了多数实际使用遇到的复杂场景功能,比如背景模糊、展现动画、收起动画等。 这些功能大多数都是可以由您自行定制或者决定是否执行,
BasePopup#onCreateContentView()):
onCreateShowAnimation() 该方法决定您的PopupWindow将会以怎样的动画展示出来,可以返回为 nullonCreateDismissAnimation() 该方法决定您的PopupWindow将会以怎样的动画小时,可以返回为 nullBasePopup#onCreateContentView() 该方法决定您的PopupWindow的contentView,一般不可以返回空值【NotNull】类似于PopupWindow.setContentView(View),
但建议使用基类方法createPopupById(int)
在您定义好基本的几个方法后,您可以创建PopupWindow实例并直接调用showPopupWindow()方法即可
example:
DemoPopup popup = new DemoPopup(context);
popup.showPopupWindow();
setBackgroundColor(int)来制定,默认情况下,该颜色为#8f000000setAlignBackground(boolean)把Mask层对齐到与您的PopupWindow主体一致setBlurBackgroundEnable(boolean)为true,默认情况下模糊对象是当前decorView,如果您需要针对模糊某个View,
请设置setBlurOption(PopupBlurOption)以及PopupBlurOption.setBlurView(View)传入。setBlurBackgroundEnable(boolean, OnBlurOptionInitListener),在BasePopupWindow.OnBlurOptionInitListener.onCreateBlurOption(PopupBlurOption)中进行修改
重大版本修正记录:
| 限定符和类型 | 类和说明 |
|---|---|
static class |
BasePopupWindow.CalledFromWrongThreadException |
static class |
BasePopupWindow.GravityMode |
static interface |
BasePopupWindow.KeyEventListener
key事件,如果返回true,则不再回调BasePopupWindow的onDispatchKeyEvent
|
static interface |
BasePopupWindow.OnBeforeShowCallback |
static interface |
BasePopupWindow.OnBlurOptionInitListener |
static class |
BasePopupWindow.OnDismissListener |
static interface |
BasePopupWindow.OnPopupWindowShowListener
当PopupWindow显示在Window上回回调给该接口监听(如果设置了监听的话)
|
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
DEFAULT_BACKGROUND_COLOR |
static int |
FLAG_KEYBOARD_ALIGN_TO_ROOT |
static int |
FLAG_KEYBOARD_ALIGN_TO_VIEW |
static int |
FLAG_KEYBOARD_ANIMATE_ALIGN |
static int |
FLAG_KEYBOARD_FORCE_ADJUST |
static int |
FLAG_KEYBOARD_IGNORE_OVER |
static int |
MATCH_PARENT |
static int |
WRAP_CONTENT |
| 构造器和说明 |
|---|
BasePopupWindow(Context context)
最常用的构造器,传入context情况下会寻找其Activity作为依附的WindowToken,如果获取不到(如ApplicationContext)则取当前应用最顶层Activity作为依附的WindowToken
此时PopupWindow的层级与Activity的Window一致,无法显示在其他Window上方(如弹出一个Dialog再弹出一个PopupWindow,此时PopupWindow显示在Dialog下层)
该构造器构造的BasePopup可以在Dialog或DialogFragment下弹出
|
BasePopupWindow(Context context,
int width,
int height) |
BasePopupWindow(Fragment fragment)
构造器传入Fragment情况下会寻找其RootView作为依附的WindowToken,如果获取不到则取当前fragment所属Activity作为依附的WindowToken
此时PopupWindow的层级与Fragment的Window一致,无法显示在其他Window上方(如弹出一个Dialog再弹出一个PopupWindow,此时PopupWindow显示在Dialog下层)
该构造器构造的BasePopup可以在DialogFragment或者依附于Activity的Fragment下弹出,其余情况不能弹出
|
BasePopupWindow(Fragment fragment,
int width,
int height) |
| 限定符和类型 | 方法和说明 |
|---|---|
BasePopupWindow |
bindLifecycleOwner(LifecycleOwner lifecycleOwner) |
int |
computeGravity(Rect popupRect,
Rect anchorRect) |
View |
createPopupById(int layoutId)
这个方法封装了LayoutInflater.from(context).inflate,方便您设置PopupWindow所用的xml
|
protected float |
dipToPx(float dip) |
void |
dismiss()
取消一个PopupWindow,如果有退出动画,PopupWindow的消失将会在动画结束后执行
|
void |
dismiss(boolean animateDismiss)
取消一个PopupWindow,如果有退出动画,PopupWindow的消失将会在动画结束后执行
|
void |
dismissWithOutAnimate()
已过时。
请使用
dismiss(boolean) |
<T extends View> |
findViewById(int id)
还在用View.findViewById么,,,不如试试这款?
|
View |
getContentView()
获取PopupWindow的根布局
|
Activity |
getContext()
获取context,请留意是否为空
null
|
Animation |
getDismissAnimation() |
Animator |
getDismissAnimator() |
View |
getDisplayAnimateView()
|
int |
getHeight()
获取PoupWindow的高度,如果PopupWindow没显示,则返回0
|
int |
getMaskOffsetX() |
int |
getMaskOffsetY() |
int |
getOffsetX() |
int |
getOffsetY() |
BasePopupWindow.OnBeforeShowCallback |
getOnBeforeShowCallback() |
BasePopupWindow.OnDismissListener |
getOnDismissListener() |
Drawable |
getPopupBackground()
获取当前PopupWindow背景
|
int |
getPopupGravity() |
PopupWindow |
getPopupWindow()
获取PopupWindow实例
|
int |
getPreMeasureHeight()
获取预测量的高度,需要先执行
preMeasure(int, int) |
int |
getPreMeasureWidth()
获取预测量的宽度,需要先执行
preMeasure(int, int) |
Animation |
getShowAnimation() |
Animator |
getShowAnimator() |
int |
getWidth()
获取PoupWindow的宽度,如果PopupWindow没显示,则返回0
|
boolean |
isAllowDismissWhenTouchOutside() |
boolean |
isAllowInterceptTouchEvent()
已过时。
please use
isOutSideTouchable() |
boolean |
isAutoLocatePopup() |
boolean |
isOutSideTouchable()
外部是否可以点击
|
boolean |
isPopupFadeEnable()
当前PopupWindow是否设置了淡入淡出效果
|
boolean |
isShowing()
PopupWindow是否处于展示状态
|
boolean |
isTouchable() |
BasePopupWindow |
linkTo(View anchorView)
允许PopupWindow跟某个anchorView关联,其位置,可视性将会跟anchorView同步>
WARN:非常不建议在anchorView频繁变化的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
onAnchorBottom()
在anchorView下方显示,autoLocatePopup为true时适用
|
void |
onAnchorBottom(View mPopupView,
View anchorView)
已过时。
因为contentView和anchorView应由用户自行保存决定,此处不再返回
|
void |
onAnchorTop()
在anchorView上方显示,autoLocatePopup为true时适用
|
void |
onAnchorTop(View mPopupView,
View anchorView)
已过时。
因为contentView和anchorView应由用户自行保存决定,此处不再返回
|
boolean |
onBackPressed()
捕捉返回键事件
|
boolean |
onBeforeDismiss() |
boolean |
onBeforeShow() |
protected View |
onCreateAnimateView()
|
abstract View |
onCreateContentView()
返回一个contentView以作为PopupWindow的contentView
强烈建议使用 createPopupById(int),该方法支持读取View的xml布局参数,否则可能会出现与布局不一样的展示从而必须手动传入宽高等参数 |
protected Animation |
onCreateDismissAnimation()
该方法决定您的PopupWindow将会以怎样的动画消失,可以返回为
null
如果返回不为空,则在返回动画播放结束后触发 PopupWindow.dismiss()
本类提供一些简单的动画方法:
AnimationHelper:快速创建动画
如果需要用到属性动画,请覆写onCreateDismissAnimator() ()} |
protected Animation |
onCreateDismissAnimation(int width,
int height) |
protected Animator |
onCreateDismissAnimator()
|
protected Animator |
onCreateDismissAnimator(int width,
int height) |
protected Animation |
onCreateShowAnimation()
该方法决定您的PopupWindow将会以怎样的动画展示出来,可以返回为
null
本类提供一些简单的动画方法:
AnimationHelper:快速创建动画
如果需要用到属性动画,请覆写onCreateShowAnimator() |
protected Animation |
onCreateShowAnimation(int width,
int height) |
protected Animator |
onCreateShowAnimator()
|
protected Animator |
onCreateShowAnimator(int width,
int height) |
void |
onDestroy() |
void |
onDismiss() |
boolean |
onDispatchKeyEvent(KeyEvent event)
捕捉keyevent
|
boolean |
onInterceptTouchEvent(MotionEvent event)
捕捉interceptTouchEvent
|
protected void |
onLogInternal(String msg)
日志输出口
|
boolean |
onOutSideTouch()
PopupWindow外的事件点击回调,请注意您的PopupWindow大小
|
void |
onPopupLayout(Rect popupRect,
Rect anchorRect)
返回BasePopup布局中的位置信息
|
protected void |
onShowError(Exception e) |
void |
onShowing()
在PopupWindow显示后回调该方法
|
boolean |
onTouchEvent(MotionEvent event)
捕捉touchevent
|
void |
onViewCreated(View contentView)
当ContentView创建的时候回调该方法,ContentView指
onCreateContentView()返回的值 |
void |
onWindowFocusChanged(View popupDecorViewProxy,
boolean hasWindowFocus) |
void |
preMeasure(int width,
int height)
预测量,测量结果未必准确~
|
BasePopupWindow |
setAdjustInputMethod(boolean needAdjust)
PopupWindow是否需要自适应输入法,为输入法弹出让出区域
|
BasePopupWindow |
setAdjustInputMethod(boolean needAdjust,
int flag)
PopupWindow是否需要自适应输入法,为输入法弹出让出区域
|
BasePopupWindow |
setAdjustInputMode(int flag)
设置PopupWindow适配输入法的适配模式
|
BasePopupWindow |
setAdjustInputMode(int viewId,
int flag)
设置PopupWindow适配输入法的适配模式
|
BasePopupWindow |
setAdjustInputMode(View alignTarget,
int flag)
设置PopupWindow适配输入法的适配模式
|
BasePopupWindow |
setAlignBackground(boolean isAlignBackground)
设置PopupWindow的背景是否对齐到PopupWindow。
|
BasePopupWindow |
setAlignBackgroundGravity(int gravity)
设置PopupWindow的背景对齐PopupWindow的方式,请传入
Gravity中的值
|
BasePopupWindow |
setAllowDismissWhenTouchOutside(boolean dismissWhenTouchOutside)
已过时。
please use
setOutSideDismiss(boolean) instead |
BasePopupWindow |
setAllowInterceptTouchEvent(boolean touchable)
已过时。
please use
setOutSideTouchable(boolean) instead |
BasePopupWindow |
setAutoLocatePopup(boolean isAutoLocatePopup)
是否自动设置PopupWindow位置
在设定了clipToScreen的情况下 #setClipToScreen(boolean)
如果当前屏幕不足以完整显示您的PopupWindow,则PopupWindow会自行布置在其镜像位置。 |
BasePopupWindow |
setAutoShowInputMethod(boolean autoShow)
PopupWindow在展示的时候自动打开输入法
|
BasePopupWindow |
setAutoShowInputMethod(EditText editText,
boolean autoShow)
PopupWindow在展示的时候自动打开输入法,在传入参数时请务必传入
EditText
|
BasePopupWindow |
setBackground(Drawable background)
设置PopupWindow背景Drawable,默认颜色为#8f000000
|
BasePopupWindow |
setBackground(int drawableIds)
设置PopupWindow背景Drawable,默认颜色为#8f000000
|
BasePopupWindow |
setBackgroundColor(int color)
设置PopupWindow背景颜色,默认颜色为#8f000000
|
BasePopupWindow |
setBackgroundView(View backgroundView)
设置PopupWindow背景View,因为背景全屏的原因,该View将会被强制设为MATCH_PARENT/MATCH_PARENT
|
BasePopupWindow |
setBackPressEnable(boolean backPressEnable)
是否允许PopupWindow响应返回键并dismiss
|
BasePopupWindow |
setBlurBackgroundEnable(boolean blurBackgroundEnable)
设置PopupWindow弹出时是否模糊背景。
|
BasePopupWindow |
setBlurBackgroundEnable(boolean blurBackgroundEnable,
BasePopupWindow.OnBlurOptionInitListener optionInitListener)
设置PopupWindow弹出时是否模糊背景。
|
BasePopupWindow |
setBlurOption(PopupBlurOption option)
|
BasePopupWindow |
setClipChildren(boolean clipChildren)
该参数决定popupWindow是否被限制在绘制边界
true:PopupWindow将会被限制边界,其动画不可突破其边界 false:PopupWindow将不会被限制绘制边界,其动画可突破其边界 |
static void |
setDebugMode(boolean debugMode) |
BasePopupWindow |
setDismissAnimation(Animation dismissAnimation)
设置退出PopupWindow的动画,详情参考
onCreateDismissAnimation()
|
BasePopupWindow |
setDismissAnimator(Animator dismissAnimator)
设置退出PopupWindow的动画,详情参考
onCreateDismissAnimator()
|
BasePopupWindow |
setFitSize(boolean canResize)
是否允许BasePopup自动调整大小
在
#setClipToScreen(boolean)为true的情况下,BasePopup会针对剩余空间来调整Popup的大小,因此可能出现实际显示过小的情况 |
BasePopupWindow |
setHeight(int height) |
BasePopupWindow |
setHeightAsAnchorView(boolean heightAsAnchor)
basepopup高度跟随anchorView
|
BasePopupWindow |
setKeyEventListener(BasePopupWindow.KeyEventListener keyEventListener)
设置key event
|
BasePopupWindow |
setLayoutDirection(int layoutDirection)
设置layoutdirection
|
BasePopupWindow |
setMaskOffsetX(int offsetX)
设定蒙层x位置的偏移量
|
BasePopupWindow |
setMaskOffsetY(int offsetY)
设定蒙层y位置的偏移量
|
BasePopupWindow |
setMaskViewDismissAnimation(Animation animation)
设置背景蒙层退出的动画,如果为空,则蒙层不显示动画,直接消失
|
BasePopupWindow |
setMaskViewShowAnimation(Animation animation)
设置背景蒙层显示的动画,如果为空,则蒙层不显示动画,直接弹出
|
BasePopupWindow |
setMaxHeight(int maxHeight)
设置BasePopup最大高度
|
BasePopupWindow |
setMaxWidth(int maxWidth)
设置BasePopup最大宽度
|
BasePopupWindow |
setMinHeight(int minHeight)
设置BasePopup最小高度
|
BasePopupWindow |
setMinWidth(int minWidth)
设置BasePopup最小宽度
|
BasePopupWindow |
setOffsetX(int offsetX)
设定x位置的偏移量(中心点在popup的左上角)
|
BasePopupWindow |
setOffsetY(int offsetY)
设定y位置的偏移量(中心点在popup的左上角)
|
BasePopupWindow |
setOnBeforeShowCallback(BasePopupWindow.OnBeforeShowCallback mOnBeforeShowCallback)
当您设置了
BasePopupWindow.OnBeforeShowCallback监听之后,在您调用{
showPopupWindow()
showPopupWindow(int)
showPopupWindow(View)
}
任意一个方法,在show之前回回调到该监听器。 |
BasePopupWindow |
setOnDismissListener(BasePopupWindow.OnDismissListener onDismissListener)
设置dismiss监听
|
BasePopupWindow |
setOnKeyboardChangeListener(KeyboardUtils.OnKeyboardChangeListener listener)
设置键盘监听回调
|
BasePopupWindow |
setOnPopupWindowShowListener(BasePopupWindow.OnPopupWindowShowListener onPopupWindowShowListener)
设置PopupWindow显示的监听
|
BasePopupWindow |
setOutSideDismiss(boolean outSideDismiss)
是否允许点击PopupWindow外部时触发dismiss
dismiss popup when touch outside from popup |
BasePopupWindow |
setOutSideTouchable(boolean touchable)
是否允许PopupWindow外部响应事件
如果允许拦截事件,则PopupWindow外部无法响应事件。 |
BasePopupWindow |
setOverlayNavigationBar(boolean overlay)
允许PopupWindow覆盖导航栏
|
BasePopupWindow |
setOverlayNavigationBarMode(int mode)
设置覆盖导航栏的模式
|
BasePopupWindow |
setOverlayStatusbar(boolean overlay)
允许PopupWindow覆盖状态栏
|
BasePopupWindow |
setOverlayStatusbarMode(int mode)
设置覆盖状态栏的模式
|
BasePopupWindow |
setPopupAnimationStyle(int animationStyleRes)
设置PopupWindow的动画style针对PopupWindow整体的Window哦
通常情况下,请使用 onCreateDismissAnimation() or onCreateShowAnimator()
|
BasePopupWindow |
setPopupFadeEnable(boolean isPopupFadeAnimate)
当传入true,你的PopupWindow将会淡入显示,淡出消失。
|
BasePopupWindow |
setPopupGravity(BasePopupWindow.GravityMode mode,
int popupGravity)
设置参考方向
Gravity
不跟anchorView联系的情况下,gravity意味着在整个view中的方位 showPopupWindow()
如果跟anchorView联系,gravity意味着以anchorView为中心的方位showPopupWindow(View)
|
BasePopupWindow |
setPopupGravity(int popupGravity)
设置参考方向
Gravity
不跟anchorView联系的情况下,gravity意味着在整个view中的方位 showPopupWindow()
如果跟anchorView联系,gravity意味着以anchorView为中心的方位showPopupWindow(View)
|
BasePopupWindow |
setPopupGravityMode(BasePopupWindow.GravityMode mode)
设置参考模式
Gravity |
BasePopupWindow |
setPopupGravityMode(BasePopupWindow.GravityMode horizontalMode,
BasePopupWindow.GravityMode verticalMode)
分别设置垂直方向与水平方向参考模式
Gravity |
BasePopupWindow |
setPopupWindowFullScreen(boolean isFullScreen)
已过时。
|
BasePopupWindow |
setShowAnimation(Animation showAnimation)
设置展示PopupWindow的动画,详情参考
onCreateShowAnimation()
|
BasePopupWindow |
setShowAnimator(Animator showAnimator)
设置展示PopupWindow的动画,详情参考
onCreateShowAnimator()
|
BasePopupWindow |
setShowKeybaordDelay(long delay)
设置输入法弹出延时
|
BasePopupWindow |
setSoftInputMode(int softInputMode)
已过时。
|
BasePopupWindow |
setTouchable(boolean touchable)
设置是否允Popup是否响应事件,如果不响应,则事件会穿透Popup
|
protected void |
setViewClickListener(View.OnClickListener listener,
View... views)
这个方法用于简化您为View设置OnClickListener事件,多个View将会使用同一个点击事件
|
BasePopupWindow |
setWidth(int width) |
BasePopupWindow |
setWidthAsAnchorView(boolean widthAsAnchor)
basepopup宽度跟随anchorview
|
void |
showPopupWindow()
调用这个方法时,将会展示PopupWindow。
|
void |
showPopupWindow(int anchorViewResid)
已过时。
please use
showPopupWindow(View) |
void |
showPopupWindow(int x,
int y)
调用这个方法时,将会在指定位置弹出PopupWindow。
|
void |
showPopupWindow(View anchorView)
调用这个方法时,将会展示PopupWindow。
|
BasePopupWindow |
syncMaskAnimationDuration(boolean sync) |
void |
update()
啥都不干,单纯的update,简单的说,就是更新你所设置的所有东西~
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
update(float width,
float height)
更新PopupWindow的宽高
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
update(int x,
int y)
在指定位置更新PopupWindow位置或大小等信息。
|
void |
update(int x,
int y,
float width,
float height)
在指定位置更新PopupWindow位置或大小等信息。
|
void |
update(View anchorView)
参考anchorView更新PopupWindow位置或大小等信息。
|
BasePopupWindow |
updateKeyboardAlign()
(手动)更新键盘对齐
|
public static int DEFAULT_BACKGROUND_COLOR
public static final int FLAG_KEYBOARD_ALIGN_TO_VIEW
public static final int FLAG_KEYBOARD_ALIGN_TO_ROOT
public static final int FLAG_KEYBOARD_IGNORE_OVER
public static final int FLAG_KEYBOARD_ANIMATE_ALIGN
public static final int FLAG_KEYBOARD_FORCE_ADJUST
public static final int MATCH_PARENT
public static final int WRAP_CONTENT
public BasePopupWindow(Context context)
public BasePopupWindow(Context context, int width, int height)
public BasePopupWindow(Fragment fragment)
public BasePopupWindow(Fragment fragment,
int width,
int height)
public BasePopupWindow bindLifecycleOwner(LifecycleOwner lifecycleOwner)
public void onViewCreated(View contentView)
onCreateContentView()返回的值public abstract View onCreateContentView()
返回一个contentView以作为PopupWindow的contentView
createPopupById(int),该方法支持读取View的xml布局参数,否则可能会出现与布局不一样的展示从而必须手动传入宽高等参数protected Animation onCreateShowAnimation()
该方法决定您的PopupWindow将会以怎样的动画展示出来,可以返回为 null
本类提供一些简单的动画方法:
AnimationHelper:快速创建动画
如果需要用到属性动画,请覆写onCreateShowAnimator()
protected Animation onCreateShowAnimation(int width, int height)
protected Animation onCreateDismissAnimation()
该方法决定您的PopupWindow将会以怎样的动画消失,可以返回为 null
如果返回不为空,则在返回动画播放结束后触发PopupWindow.dismiss()
本类提供一些简单的动画方法:
AnimationHelper:快速创建动画
如果需要用到属性动画,请覆写onCreateDismissAnimator() ()}
protected Animation onCreateDismissAnimation(int width, int height)
protected Animator onCreateShowAnimator()
该方法决定您的PopupWindow将会以怎样的动画展示出来(返回 Animator),可以返回为 null
功能详情请看onCreateShowAnimation()
protected Animator onCreateShowAnimator(int width, int height)
protected View onCreateAnimateView()
通过该方法您可以指定您的PopupWindow显示动画用于哪个View(onCreateShowAnimation()/onCreateShowAnimator())
可以返回为空 null
protected Animator onCreateDismissAnimator()
该方法决定您的PopupWindow将会以怎样的动画消失(返回 Animator),可以返回为 null
功能详情请看onCreateDismissAnimation() ()}
protected Animator onCreateDismissAnimator(int width, int height)
public BasePopupWindow setPopupFadeEnable(boolean isPopupFadeAnimate)
当传入true,你的PopupWindow将会淡入显示,淡出消失。
与onCreateShowAnimation()/onCreateDismissAnimation()不同的是,该方法为Window层级服务,固定Style
isPopupFadeAnimate - true for apply anim stylepublic boolean isPopupFadeEnable()
当前PopupWindow是否设置了淡入淡出效果
public BasePopupWindow setPopupAnimationStyle(int animationStyleRes)
设置PopupWindow的动画style针对PopupWindow整体的Window哦
通常情况下,请使用onCreateDismissAnimation() or onCreateShowAnimator()
public void showPopupWindow()
调用这个方法时,将会展示PopupWindow。
如果onCreateShowAnimation() or onCreateShowAnimator()其中之一返回不为空,
则在PopupWindow展示后为onCreateAnimateView() 指定的View执行动画
您可以在Activity.onCreate(Bundle)里面使用该方法,本方法在无法展示时会重试3次,如果3次都无法展示,则失败。
@Deprecated public void showPopupWindow(int anchorViewResid)
showPopupWindow(View)
传入anchorView的ViewId,方法详情showPopupWindow(View)
anchorViewResid - anchorView的ViewIdpublic void showPopupWindow(View anchorView)
调用这个方法时,将会展示PopupWindow。
showPopupWindow()
anchorView - 锚点View,PopupWindow将会显示在其下方public void showPopupWindow(int x,
int y)
调用这个方法时,将会在指定位置弹出PopupWindow。
其他方法详情参考showPopupWindow()
x - 坐标轴xy - 坐标轴ypublic void update()
啥都不干,单纯的update,简单的说,就是更新你所设置的所有东西~
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。
public void update(View anchorView)
参考anchorView更新PopupWindow位置或大小等信息。
该方法跟anchorView关联,即您的gravity,offset等会跟随anchorView变化而变化
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。
anchorView - 被参考的anchorViewpublic void update(int x,
int y)
在指定位置更新PopupWindow位置或大小等信息。
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。
x - 目标位置x坐标y - 目标位置y坐标public void update(float width,
float height)
更新PopupWindow的宽高
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。
width - 宽度height - 高度public void update(int x,
int y,
float width,
float height)
在指定位置更新PopupWindow位置或大小等信息。
x - 目标位置x坐标y - 目标位置y坐标width - 宽度height - 高度public boolean onBeforeShow()
public boolean onBeforeDismiss()
protected void onShowError(Exception e)
public BasePopupWindow setAdjustInputMethod(boolean needAdjust)
PopupWindow是否需要自适应输入法,为输入法弹出让出区域
needAdjust - public BasePopupWindow setAdjustInputMethod(boolean needAdjust, int flag)
PopupWindow是否需要自适应输入法,为输入法弹出让出区域
needAdjust - flag - The desired mode, see
WindowManager.LayoutParams.softInputMode
for the full listpublic BasePopupWindow setAdjustInputMode(int flag)
设置PopupWindow适配输入法的适配模式
flag - FLAG_KEYBOARD_ALIGN_TO_ROOT,键盘适配对齐到整个popup content viewFLAG_KEYBOARD_ALIGN_TO_VIEW,键盘适配对齐到指定的view,需要传入viewidFLAG_KEYBOARD_IGNORE_OVER,键盘适配仅作用于无法完整显示的情况FLAG_KEYBOARD_ANIMATE_ALIGN,键盘是否动画适配FLAG_KEYBOARD_FORCE_ADJUST,是否强制适配输入法public BasePopupWindow setAdjustInputMode(int viewId, int flag)
设置PopupWindow适配输入法的适配模式
viewId - keyboard对齐的View idflag - FLAG_KEYBOARD_ALIGN_TO_ROOT,键盘适配对齐到整个popup content viewFLAG_KEYBOARD_ALIGN_TO_VIEW,键盘适配对齐到指定的view,需要传入viewidFLAG_KEYBOARD_IGNORE_OVER,键盘适配仅作用于无法完整显示的情况FLAG_KEYBOARD_ANIMATE_ALIGN,键盘是否动画适配FLAG_KEYBOARD_FORCE_ADJUST,是否强制适配输入法public BasePopupWindow setAdjustInputMode(View alignTarget, int flag)
设置PopupWindow适配输入法的适配模式
alignTarget - keyboard对齐的Vieflag - FLAG_KEYBOARD_ALIGN_TO_ROOT,键盘适配对齐到整个popup content viewFLAG_KEYBOARD_ALIGN_TO_VIEW,键盘适配对齐到指定的view,需要传入view,传入view的时候将会优先于传入viewidFLAG_KEYBOARD_IGNORE_OVER,键盘适配仅作用于无法完整显示的情况FLAG_KEYBOARD_ANIMATE_ALIGN,键盘是否动画适配FLAG_KEYBOARD_FORCE_ADJUST,是否强制适配输入法public BasePopupWindow setAutoShowInputMethod(boolean autoShow)
PopupWindow在展示的时候自动打开输入法
public BasePopupWindow setAutoShowInputMethod(EditText editText, boolean autoShow)
PopupWindow在展示的时候自动打开输入法,在传入参数时请务必传入EditText
@Deprecated public BasePopupWindow setSoftInputMode(int softInputMode)
softInputMode - The desired mode, see
WindowManager.LayoutParams.softInputMode
for the full listpublic BasePopupWindow setShowKeybaordDelay(long delay)
delay - 单位:毫秒,如果小于0则会被置为0public BasePopupWindow setBackPressEnable(boolean backPressEnable)
是否允许PopupWindow响应返回键并dismiss
public View createPopupById(int layoutId)
这个方法封装了LayoutInflater.from(context).inflate,方便您设置PopupWindow所用的xml
layoutId - reference of layoutpublic <T extends View> T findViewById(int id)
还在用View.findViewById么,,,不如试试这款?
id - the ID to search fornull otherwise@Deprecated public BasePopupWindow setPopupWindowFullScreen(boolean isFullScreen)
允许PopupWindow覆盖状态栏
use setOverlayStatusbar(boolean) instead
public BasePopupWindow setOverlayStatusbar(boolean overlay)
public BasePopupWindow setOverlayStatusbarMode(int mode)
mode - BasePopupFlag.OVERLAY_CONTENT或者BasePopupFlag.OVERLAY_MASKBasePopupFlag.OVERLAY_CONTENT,则内容(onCreateContentView())可以覆盖导航栏,蒙层不可以BasePopupFlag.OVERLAY_MASK,则蒙层可以覆盖导航栏,内容不可以BasePopupFlag.OVERLAY_CONTENT|BasePopupFlag.OVERLAY_MASK,则两者皆可public BasePopupWindow setOverlayNavigationBar(boolean overlay)
public BasePopupWindow setOverlayNavigationBarMode(int mode)
mode - BasePopupFlag.OVERLAY_CONTENT或者BasePopupFlag.OVERLAY_MASKBasePopupFlag.OVERLAY_CONTENT,则内容(onCreateContentView())可以覆盖导航栏,蒙层不可以BasePopupFlag.OVERLAY_MASK,则蒙层可以覆盖导航栏,内容不可以BasePopupFlag.OVERLAY_CONTENT|BasePopupFlag.OVERLAY_MASK,则两者皆可public BasePopupWindow setBackgroundColor(int color)
设置PopupWindow背景颜色,默认颜色为#8f000000
color - 背景颜色public BasePopupWindow setBackground(int drawableIds)
设置PopupWindow背景Drawable,默认颜色为#8f000000
drawableIds - 背景Drawable idpublic BasePopupWindow setBackground(Drawable background)
设置PopupWindow背景Drawable,默认颜色为#8f000000
background - 背景Drawablepublic BasePopupWindow setBackgroundView(View backgroundView)
设置PopupWindow背景View,因为背景全屏的原因,该View将会被强制设为MATCH_PARENT/MATCH_PARENT
backgroundView - 背景Viewpublic Drawable getPopupBackground()
获取当前PopupWindow背景
public BasePopupWindow setBlurBackgroundEnable(boolean blurBackgroundEnable)
设置PopupWindow弹出时是否模糊背景。
在使用模糊背景前,您可以通过setBlurOption(PopupBlurOption)传入模糊配置。
本方法默认模糊当前Activity的DecorView
blurBackgroundEnable - true for blur decorViewpublic BasePopupWindow setBlurBackgroundEnable(boolean blurBackgroundEnable, BasePopupWindow.OnBlurOptionInitListener optionInitListener)
设置PopupWindow弹出时是否模糊背景。
在使用模糊背景前,您可以通过setBlurOption(PopupBlurOption)传入模糊配置。
本方法允许您传入一个初始化监听,您可以在BasePopupWindow.OnBlurOptionInitListener.onCreateBlurOption(PopupBlurOption)中进行展示前的最后一次修改
blurBackgroundEnable - true for blur decorViewoptionInitListener - 初始化回调public BasePopupWindow setBlurOption(PopupBlurOption option)
应用模糊配置,更多详情请参考setBlurBackgroundEnable(boolean)或者setBlurBackgroundEnable(boolean, OnBlurOptionInitListener)
option - 模糊配置protected void setViewClickListener(View.OnClickListener listener, View... views)
public boolean isShowing()
public BasePopupWindow.OnDismissListener getOnDismissListener()
public BasePopupWindow setOnPopupWindowShowListener(BasePopupWindow.OnPopupWindowShowListener onPopupWindowShowListener)
public BasePopupWindow setOnDismissListener(BasePopupWindow.OnDismissListener onDismissListener)
设置dismiss监听
onDismissListener - 监听器public BasePopupWindow.OnBeforeShowCallback getOnBeforeShowCallback()
public BasePopupWindow setOnBeforeShowCallback(BasePopupWindow.OnBeforeShowCallback mOnBeforeShowCallback)
mOnBeforeShowCallback - BasePopupWindow.OnBeforeShowCallback.onBeforeShow(View, View, boolean)public BasePopupWindow setShowAnimation(Animation showAnimation)
设置展示PopupWindow的动画,详情参考onCreateShowAnimation()
showAnimation - 展示动画public Animation getShowAnimation()
public BasePopupWindow setShowAnimator(Animator showAnimator)
设置展示PopupWindow的动画,详情参考onCreateShowAnimator()
showAnimator - 展示动画public Animator getShowAnimator()
public BasePopupWindow setDismissAnimation(Animation dismissAnimation)
设置退出PopupWindow的动画,详情参考onCreateDismissAnimation()
dismissAnimation - 退出动画public Animation getDismissAnimation()
public BasePopupWindow setDismissAnimator(Animator dismissAnimator)
设置退出PopupWindow的动画,详情参考onCreateDismissAnimator()
dismissAnimator - 退出动画public Animator getDismissAnimator()
public Activity getContext()
获取context,请留意是否为空null
nullpublic View getContentView()
获取PopupWindow的根布局
#onCreateContentView(),该布局在这里初始化。public View getDisplayAnimateView()
public PopupWindow getPopupWindow()
public int getOffsetX()
public int getMaskOffsetX()
public BasePopupWindow setOffsetX(int offsetX)
offsetX - public BasePopupWindow setMaskOffsetX(int offsetX)
offsetX - public int getOffsetY()
public int getMaskOffsetY()
public BasePopupWindow setOffsetY(int offsetY)
offsetY - public BasePopupWindow setMaskOffsetY(int offsetY)
offsetY - public int getPopupGravity()
public BasePopupWindow setPopupGravity(int popupGravity)
设置参考方向 Gravity
showPopupWindow()showPopupWindow(View)popupGravity - public BasePopupWindow setPopupGravity(BasePopupWindow.GravityMode mode, int popupGravity)
设置参考方向 Gravity
showPopupWindow()showPopupWindow(View)mode - public BasePopupWindow setPopupGravityMode(BasePopupWindow.GravityMode mode)
Gravitymode - public BasePopupWindow setPopupGravityMode(BasePopupWindow.GravityMode horizontalMode, BasePopupWindow.GravityMode verticalMode)
Gravitymode - public boolean isAutoLocatePopup()
public BasePopupWindow setAutoLocatePopup(boolean isAutoLocatePopup)
是否自动设置PopupWindow位置
在设定了clipToScreen的情况下#setClipToScreen(boolean)
如果当前屏幕不足以完整显示您的PopupWindow,则PopupWindow会自行布置在其镜像位置。
比如当前PopupWindow显示在某个View的下方,而屏幕下方不够位置展示完整改PopupWindow, 当本设置为true,PopupWindow将会显示在原来的View的上方以满足完整显示PopupWindow的情况。
setOffsetY(int),则对应的偏移量也是在其适配后的位置生效
isAutoLocatePopup - 是否自适配public int getHeight()
获取PoupWindow的高度,如果PopupWindow没显示,则返回0
#preMeasurePopupView(int, int)public int getWidth()
获取PoupWindow的宽度,如果PopupWindow没显示,则返回0
#preMeasurePopupView(int, int)public void preMeasure(int width,
int height)
public int getPreMeasureWidth()
preMeasure(int, int)public int getPreMeasureHeight()
preMeasure(int, int)@Deprecated public BasePopupWindow setAllowDismissWhenTouchOutside(boolean dismissWhenTouchOutside)
setOutSideDismiss(boolean) instead是否允许点击PopupWindow外部时触发dismiss
dismissWhenTouchOutside - true for allowpublic BasePopupWindow setOutSideDismiss(boolean outSideDismiss)
是否允许点击PopupWindow外部时触发dismiss
outSideDismiss - true for allow@Deprecated public BasePopupWindow setAllowInterceptTouchEvent(boolean touchable)
setOutSideTouchable(boolean) instead
是否允许点击PopupWindow拦截事件。
如果允许拦截事件,则PopupWindow外部无法响应事件。
touchable - public BasePopupWindow setOutSideTouchable(boolean touchable)
是否允许PopupWindow外部响应事件
如果允许拦截事件,则PopupWindow外部无法响应事件。
touchable - public BasePopupWindow setClipChildren(boolean clipChildren)
clipChildren - 默认为truepublic boolean isAllowDismissWhenTouchOutside()
@Deprecated public boolean isAllowInterceptTouchEvent()
isOutSideTouchable()public boolean isOutSideTouchable()
public BasePopupWindow setAlignBackground(boolean isAlignBackground)
设置PopupWindow的背景是否对齐到PopupWindow。
默认情况下,PopupWindow背景都是铺满整个屏幕的。
但在某些情况下您可能在PopupWindow之上不需要展示背景,这时候您可以调用这个方法来强制Background对齐到PopupWindow的顶部。
isAlignBackground - 是否对齐背景public BasePopupWindow setAlignBackgroundGravity(int gravity)
设置PopupWindow的背景对齐PopupWindow的方式,请传入Gravity中的值
gravity - 请传入Gravity中的值,传入Gravity.NO_GRAVITY则意味着不对齐public BasePopupWindow linkTo(View anchorView)
允许PopupWindow跟某个anchorView关联,其位置,可视性将会跟anchorView同步>
WARN:非常不建议在anchorView频繁变化的情况下使用背景模糊,这会导致较大的性能消耗。
public BasePopupWindow setWidth(int width)
public BasePopupWindow setHeight(int height)
public BasePopupWindow setMaxWidth(int maxWidth)
public BasePopupWindow setMaxHeight(int maxHeight)
public BasePopupWindow setMinWidth(int minWidth)
public BasePopupWindow setMinHeight(int minHeight)
public BasePopupWindow setFitSize(boolean canResize)
在#setClipToScreen(boolean)为true的情况下,BasePopup会针对剩余空间来调整Popup的大小,因此可能出现实际显示过小的情况
public BasePopupWindow setMaskViewShowAnimation(Animation animation)
public BasePopupWindow setMaskViewDismissAnimation(Animation animation)
public BasePopupWindow syncMaskAnimationDuration(boolean sync)
public BasePopupWindow setOnKeyboardChangeListener(KeyboardUtils.OnKeyboardChangeListener listener)
public BasePopupWindow setKeyEventListener(BasePopupWindow.KeyEventListener keyEventListener)
public BasePopupWindow setWidthAsAnchorView(boolean widthAsAnchor)
public BasePopupWindow setHeightAsAnchorView(boolean heightAsAnchor)
public BasePopupWindow setLayoutDirection(int layoutDirection)
public BasePopupWindow setTouchable(boolean touchable)
public boolean isTouchable()
public BasePopupWindow updateKeyboardAlign()
public void dismiss()
public void dismiss(boolean animateDismiss)
animateDismiss - 传入为true,则执行退出动画后dismiss(如果有的话)@Deprecated public void dismissWithOutAnimate()
dismiss(boolean)public void onDestroy()
public boolean onDispatchKeyEvent(KeyEvent event)
event - public boolean onInterceptTouchEvent(MotionEvent event)
event - public boolean onTouchEvent(MotionEvent event)
event - public boolean onBackPressed()
public boolean onOutSideTouch()
public void onWindowFocusChanged(View popupDecorViewProxy, boolean hasWindowFocus)
public void onAnchorTop()
public void onAnchorBottom()
@Deprecated public void onAnchorTop(View mPopupView, View anchorView)
mPopupView - onCreateContentView()返回的ViewanchorView - showPopupWindow(View)传入的ViewonAnchorTop()@Deprecated public void onAnchorBottom(View mPopupView, View anchorView)
mPopupView - onCreateContentView()返回的ViewanchorView - showPopupWindow(View)传入的ViewonAnchorBottom()public void onDismiss()
onDismiss 在接口中 PopupWindow.OnDismissListenerpublic void onShowing()
protected float dipToPx(float dip)
public static void setDebugMode(boolean debugMode)
protected void onLogInternal(String msg)