在API5.1開始PopupWindow添加了以下2個(gè)方法
- void setAttachedInDecor(boolean)
- boolean isAttachedInDecor()
主要作用是為了設(shè)置PopupWindow顯示的時(shí)候是否會與StatusBar重疊(如果存在的話也包括SystemBar)
查看源碼可以看到這個(gè)屬性的默認(rèn)值是不固定的碱茁,是由系統(tǒng)版本來確定的
public void setContentView(View contentView) {
if (isShowing()) {
return;
}
mContentView = contentView;
if (mContext == null && mContentView != null) {
mContext = mContentView.getContext();
}
if (mWindowManager == null && mContentView != null) {
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
// Setting the default for attachedInDecor based on SDK version here
// instead of in the constructor since we might not have the context
// object in the constructor. We only want to set default here if the
// app hasn't already set the attachedInDecor.
if (mContext != null && !mAttachedInDecorSet) {
// Attach popup window in decor frame of parent window by default for
// {@link Build.VERSION_CODES.LOLLIPOP_MR1} or greater. Keep current
// behavior of not attaching to decor frame for older SDKs.
setAttachedInDecor(mContext.getApplicationInfo().targetSdkVersion
>= Build.VERSION_CODES.LOLLIPOP_MR1);
}
}
最終可以看到 當(dāng)這個(gè)屬性為true時(shí)為window 添加了一個(gè)新的FLAG
if (mAttachedInDecor) {
curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR;
}
需要注意在PopupWindow中部分的View可能是經(jīng)過精確計(jì)算的棕叫,但在不同版本就可能產(chǎn)生出不同的效果了耿焊,這是就可以通過setAttachedInDecor(boolean)
來做適配,也可以根據(jù)系統(tǒng)版本號來做相應(yīng)的操作。