以前对于提示类型UI用到了PopupWindow
通过构造函数或者setContentView(View contentView)可以设置其显示内容;
显示时showAtLocation(View parent, int gravity, int x, int y)必须指定父视图或者showAsDropDown(View anchor, int xoff, int yoff)相对位置视图,而我们常常在广播或者什么中处理消息时,发现很可能这些视图或者他们的父视图已经被释放掉了,从而产生异常。后面讲直接调用WindowManager实现显示,就不会存在这个现象了。
public void showAtLocation(View parent, int gravity, int x, int y) {
showAtLocation(parent.getWindowToken(), gravity, x, y);
}
public void showAtLocation(IBinder token, int gravity, int x, int y) {
.................
WindowManager.LayoutParams p = createPopupLayout(token);
p.windowAnimations = computeAnimationResource();
preparePopup(p);
................
invokePopup(p);
}
private void invokePopup(WindowManager.LayoutParams p) {
if (mContext != null) {
p.packageName = mContext.getPackageName();
}
mPopupView.setFitsSystemWindows(mLayoutInsetDecor);
mWindowManager.addView(mPopupView, p);
}
createPopupLayout负责背景处理,同时将token赋值给params的token,后期WindowManager会检查该值,但没跟踪到该值有什么作用。(求指点)
底层是由WindowManager将视图加载进入视图管理器,下次将初步分析一下WindowManager。