public abstract class BasePopupWindow extends Object implements BasePopup, 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)中进行修改
重大版本修正记录:
| Modifier and Type | Class and Description |
|---|---|
static class |
BasePopupWindow.GravityMode |
static interface |
BasePopupWindow.OnBeforeShowCallback |
static interface |
BasePopupWindow.OnBlurOptionInitListener |
static class |
BasePopupWindow.OnDismissListener |
static interface |
BasePopupWindow.OnPopupWindowShowListener
当PopupWindow显示在Window上回回调给该接口监听(如果设置了监听的话)
|
| Modifier and Type | Field and Description |
|---|---|
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 |
| Constructor and Description |
|---|
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) |
| Modifier and Type | Method and Description |
|---|---|
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()
Deprecated.
请使用
dismiss(boolean) |
<T extends View> |
findViewById(int id)
还在用View.findViewById么,,,不如试试这款?
|
View |
getContentView()
获取PopupWindow的根布局
|
Activity |
getContext()
获取context,请留意是否为空
null |
protected Animation |
getDefaultAlphaAnimation()
生成默认的AlphaAnimation
|
protected Animation |
getDefaultAlphaAnimation(boolean in)
生成默认的AlphaAnimation
|
protected Animation |
getDefaultScaleAnimation()
生成自定义ScaleAnimation
|
protected Animation |
getDefaultScaleAnimation(boolean in)
生成自定义ScaleAnimation
|
protected AnimatorSet |
getDefaultSlideFromBottomAnimationSet()
从下方滑动上来
|
Animation |
getDismissAnimation() |
Animator |
getDismissAnimator() |
View |
getDisplayAnimateView()
获取PopupWindow执行动画的View
|
int |
getHeight()
获取PoupWindow的高度。
当PopupWindow没show出来的时候高度会是0,此时则返回pre measure的高度,不一定精准 |
int |
getOffsetX() |
int |
getOffsetY() |
BasePopupWindow.OnBeforeShowCallback |
getOnBeforeShowCallback() |
BasePopupWindow.OnDismissListener |
getOnDismissListener() |
Drawable |
getPopupBackground()
获取当前PopupWindow背景
|
int |
getPopupGravity() |
PopupWindow |
getPopupWindow()
获取PopupWindow实例
|
protected Animation |
getScaleAnimation(float fromX,
float toX,
float fromY,
float toY,
int pivotXType,
float pivotXValue,
int pivotYType,
float pivotYValue)
生成ScaleAnimation
|
Animation |
getShowAnimation() |
Animator |
getShowAnimator() |
protected Animation |
getTranslateVerticalAnimation(float start,
float end,
int durationMillis)
生成TranslateAnimation(相对于parent)
|
protected Animation |
getTranslateVerticalAnimation(int start,
int end,
int durationMillis)
生成TranslateAnimation
|
int |
getWidth()
获取PoupWindow的宽度。
当popupwindow没show出来的时候高度会是0,此时则返回pre measure的宽度,不一定精准 |
boolean |
isAllowDismissWhenTouchOutside() |
boolean |
isAllowInterceptTouchEvent()
Deprecated.
please use
isOutSideTouchable() |
boolean |
isAutoLocatePopup() |
boolean |
isOutSideTouchable()
外部是否可以点击
|
boolean |
isPopupFadeEnable()
当前PopupWindow是否设置了淡入淡出效果
|
boolean |
isShowing()
PopupWindow是否处于展示状态
|
BasePopupWindow |
linkTo(View anchorView)
允许PopupWindow跟某个anchorView关联,其位置,可视性将会跟anchorView同步>
WARN:非常不建议在anchorView频繁变化的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
onAnchorBottom()
在anchorView下方显示,autoLocatePopup为true时适用
|
void |
onAnchorBottom(View mPopupView,
View anchorView)
Deprecated.
因为contentView和anchorView应由用户自行保存决定,此处不再返回
|
void |
onAnchorTop()
在anchorView上方显示,autoLocatePopup为true时适用
|
void |
onAnchorTop(View mPopupView,
View anchorView)
Deprecated.
因为contentView和anchorView应由用户自行保存决定,此处不再返回
|
boolean |
onBackPressed()
捕捉返回键事件
|
protected View |
onCreateAnimateView()
|
protected Animation |
onCreateDismissAnimation()
|
protected Animation |
onCreateDismissAnimation(int width,
int height) |
protected Animator |
onCreateDismissAnimator()
|
protected Animator |
onCreateDismissAnimator(int width,
int height) |
protected Animation |
onCreateShowAnimation()
该方法决定您的PopupWindow将会以怎样的动画展示出来,可以返回为
null |
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 boolean |
onPreShow() |
protected void |
onShowError(Exception e) |
void |
onShowing()
在PopupWindow显示后回调该方法
|
boolean |
onTouchEvent(MotionEvent event)
捕捉touchevent
|
void |
onViewCreated(View contentView)
当ContentView创建的时候回调该方法,ContentView指
BasePopup.onCreateContentView()返回的值 |
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 |
setAlignBackground(boolean isAlignBackground)
设置PopupWindow的背景是否对齐到PopupWindow。
默认情况下,PopupWindow背景都是铺满整个屏幕的。 但在某些情况下您可能在PopupWindow之上不需要展示背景,这时候您可以调用这个方法来强制Background对齐到PopupWindow的顶部。 |
BasePopupWindow |
setAlignBackgroundGravity(int gravity)
设置PopupWindow的背景对齐PopupWindow的方式,请传入
Gravity中的值
|
BasePopupWindow |
setAllowDismissWhenTouchOutside(boolean dismissWhenTouchOutside)
Deprecated.
please use
setOutSideDismiss(boolean) instead |
BasePopupWindow |
setAllowInterceptTouchEvent(boolean touchable)
Deprecated.
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弹出时是否模糊背景。
在使用模糊背景前,您可以通过 setBlurOption(PopupBlurOption)传入模糊配置。
本方法默认模糊当前Activity的DecorView |
BasePopupWindow |
setBlurBackgroundEnable(boolean blurBackgroundEnable,
BasePopupWindow.OnBlurOptionInitListener optionInitListener)
设置PopupWindow弹出时是否模糊背景。
在使用模糊背景前,您可以通过 setBlurOption(PopupBlurOption)传入模糊配置。
本方法允许您传入一个初始化监听,您可以在 BasePopupWindow.OnBlurOptionInitListener.onCreateBlurOption(PopupBlurOption)中进行展示前的最后一次修改 |
BasePopupWindow |
setBlurOption(PopupBlurOption option)
|
BasePopupWindow |
setClipChildren(boolean clipChildren)
该参数决定popupWindow是否被限制在绘制边界
|
static void |
setDebugMode(boolean debugMode) |
BasePopupWindow |
setDismissAnimation(Animation dismissAnimation)
设置退出PopupWindow的动画,详情参考
onCreateDismissAnimation() |
BasePopupWindow |
setDismissAnimator(Animator dismissAnimator)
设置退出PopupWindow的动画,详情参考
onCreateDismissAnimator() |
BasePopupWindow |
setFitSize(boolean canResize)
是否允许BasePopup自动调整大小
|
BasePopupWindow |
setHeight(int height) |
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 |
setOnPopupWindowShowListener(BasePopupWindow.OnPopupWindowShowListener onPopupWindowShowListener)
设置PopupWindow显示的监听
|
BasePopupWindow |
setOutSideDismiss(boolean outSideDismiss)
是否允许点击PopupWindow外部时触发dismiss
|
BasePopupWindow |
setOutSideTouchable(boolean touchable)
是否允许点击PopupWindow拦截事件。
如果允许拦截事件,则PopupWindow外部无法响应事件。 |
BasePopupWindow |
setOverlayStatusbar(boolean overlay) |
BasePopupWindow |
setPopupAnimationStyle(int animationStyleRes)
设置PopupWindow的动画style针对PopupWindow整体的Window哦
通常情况下,请使用 onCreateDismissAnimation() or onCreateShowAnimator() |
BasePopupWindow |
setPopupFadeEnable(boolean isPopupFadeAnimate)
当传入true,你的PopupWindow将会淡入显示,淡出消失。
与 onCreateShowAnimation()/onCreateDismissAnimation()不同的是,该方法为Window层级服务,固定Style
|
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 |
setPopupWindowFullScreen(boolean isFullScreen)
Deprecated.
|
BasePopupWindow |
setShowAnimation(Animation showAnimation)
设置展示PopupWindow的动画,详情参考
onCreateShowAnimation() |
BasePopupWindow |
setShowAnimator(Animator showAnimator)
设置展示PopupWindow的动画,详情参考
onCreateShowAnimator() |
BasePopupWindow |
setSoftInputMode(int softInputMode)
Deprecated.
|
protected void |
setViewClickListener(View.OnClickListener listener,
View... views)
这个方法用于简化您为View设置OnClickListener事件,多个View将会使用同一个点击事件
|
BasePopupWindow |
setWidth(int width) |
void |
showPopupWindow()
调用这个方法时,将会展示PopupWindow。
如果 onCreateShowAnimation() or onCreateShowAnimator()其中之一返回不为空,
则在PopupWindow展示后为onCreateAnimateView() 指定的View执行动画 |
void |
showPopupWindow(int anchorViewResid)
Deprecated.
please use
showPopupWindow(View) |
void |
showPopupWindow(int x,
int y)
调用这个方法时,将会在指定位置弹出PopupWindow。
其他方法详情参考 showPopupWindow() |
void |
showPopupWindow(View anchorView)
调用这个方法时,将会展示PopupWindow。
|
void |
update()
啥都不干,单纯的update,简单的说,就是更新你所设置的所有东西~
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
update(float width,
float height)
更新PopupWindow的宽高
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
update(int x,
int y)
在指定位置更新PopupWindow位置或大小等信息。
WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
void |
update(int x,
int y,
float width,
float height)
在指定位置更新PopupWindow位置或大小等信息。
|
void |
update(View anchorView)
参考anchorView更新PopupWindow位置或大小等信息。
该方法跟anchorView关联,即您的gravity,offset等会跟随anchorView变化而变化 WARN:非常不建议在连续update的情况下使用背景模糊,这会导致较大的性能消耗。 |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitonCreateContentViewpublic 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)
BasePopup.onCreateContentView()返回的值protected Animation onCreateShowAnimation()
该方法决定您的PopupWindow将会以怎样的动画展示出来,可以返回为 null
本类提供一些简单的动画方法:
getDefaultAlphaAnimation():得到一个默认进入的渐变动画getDefaultScaleAnimation():得到一个默认的放大缩小动画getTranslateVerticalAnimation(float, float, int) ()}:快速获取垂直方向的动画
如果需要用到属性动画,请覆写onCreateShowAnimator()
protected Animation onCreateShowAnimation(int width, int height)
protected Animation onCreateDismissAnimation()
该方法决定您的PopupWindow将会以怎样的动画消失,可以返回为 null
如果返回不为空,则在返回动画播放结束后触发PopupWindow.dismiss()
本类提供一些简单的动画方法:
getDefaultAlphaAnimation(boolean) ()}:得到一个默认进入的渐变动画getDefaultScaleAnimation(boolean) ()}:得到一个默认的放大缩小动画getTranslateVerticalAnimation(float, float, int) ()}:快速获取垂直方向的动画
如果需要用到属性动画,请覆写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 - 高度protected boolean onPreShow()
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 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 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 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 BasePopupWindow setOffsetX(int offsetX)
offsetX - public int getOffsetY()
public BasePopupWindow setOffsetY(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 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没show出来的时候高度会是0,此时则返回pre measure的高度,不一定精准
#preMeasurePopupView(int, int)public int getWidth()
获取PoupWindow的宽度。
当popupwindow没show出来的时候高度会是0,此时则返回pre measure的宽度,不一定精准
#preMeasurePopupView(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 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 onAnchorTop()
public void onAnchorBottom()
@Deprecated public void onAnchorTop(View mPopupView, View anchorView)
mPopupView - BasePopup.onCreateContentView()返回的ViewanchorView - showPopupWindow(View)传入的ViewonAnchorTop()@Deprecated public void onAnchorBottom(View mPopupView, View anchorView)
mPopupView - BasePopup.onCreateContentView()返回的ViewanchorView - showPopupWindow(View)传入的ViewonAnchorBottom()public void onDismiss()
onDismiss in interface PopupWindow.OnDismissListenerpublic void onShowing()
protected float dipToPx(float dip)
public static void setDebugMode(boolean debugMode)
protected Animation getTranslateVerticalAnimation(int start, int end, int durationMillis)
durationMillis - 动画显示时间start - 初始百分比end - 结束百分比protected Animation getTranslateVerticalAnimation(float start, float end, int durationMillis)
durationMillis - 动画显示时间start - 初始百分比(0f~1f)end - 结束百分比(0f~1f)protected Animation getScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
time=300
protected Animation getDefaultScaleAnimation()
protected Animation getDefaultScaleAnimation(boolean in)
in - true for scale inprotected Animation getDefaultAlphaAnimation()
protected Animation getDefaultAlphaAnimation(boolean in)
in - true for alpha inprotected AnimatorSet getDefaultSlideFromBottomAnimationSet()
protected void onLogInternal(String msg)