老早QA就提了個bug,說我們的popupWindow在android N (7.0)系統(tǒng)展示不對。
然后我今天有空就把這個bug修了,沒明白google為啥這次這樣改PopupWindow,可能是他們的bug,下面詳細看看這個是什么bug。
兼容性現(xiàn)象
popupWindow設置了居中或者底部對齊,但是在7.0機器是跑到頂部。
很明顯這個bug是和我們設置了Gravity有關(guān)。
展示popupWindow的函數(shù)有兩個,showAtLocation 和 update。
重點看了那兩個函數(shù)的API 24 和 API 23 的區(qū)別。
源碼分析
通過源碼分析發(fā)現(xiàn),在update函數(shù)里有一個和gravity相關(guān)的地方,很明顯是個bug。
public void update(int x, int y, int width, int height, boolean force) {
if (width >= 0) {
mLastWidth = width;
setWidth(width);
}
if (height >= 0) {
mLastHeight = height;
setHeight(height);
}
if (!isShowing() || mContentView == null) {
return;
}
final WindowManager.LayoutParams p =
(WindowManager.LayoutParams) mDecorView.getLayoutParams();
boolean update = force;
final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth;
if (width != -1 && p.width != finalWidth) {
p.width = mLastWidth = finalWidth;
update = true;
}
final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight;
if (height != -1 && p.height != finalHeight) {
p.height = mLastHeight = finalHeight;
update = true;
}
if (p.x != x) {
p.x = x;
update = true;
}
if (p.y != y) {
p.y = y;
update = true;
}
final int newAnim = computeAnimationResource();
if (newAnim != p.windowAnimations) {
p.windowAnimations = newAnim;
update = true;
}
final int newFlags = computeFlags(p.flags);
if (newFlags != p.flags) {
p.flags = newFlags;
update = true;
}
final int newGravity = computeGravity();
if (newGravity != p.gravity) {
p.gravity = newGravity;
update = true;
}
int newAccessibilityIdOfAnchor =
(mAnchor != null) ? mAnchor.get().getAccessibilityViewId() : -1;
if (newAccessibilityIdOfAnchor != p.accessibilityIdOfAnchor) {
p.accessibilityIdOfAnchor = newAccessibilityIdOfAnchor;
update = true;
}
if (update) {
setLayoutDirectionFromAnchor();
mWindowManager.updateViewLayout(mDecorView, p);
}
}
有個 computeGravity 函數(shù),我們再看看
private int computeGravity() {
int gravity = Gravity.START | Gravity.TOP;
if (mClipToScreen || mClippingEnabled) {
gravity |= Gravity.DISPLAY_CLIP_VERTICAL;
}
return gravity;
}
噗,我們發(fā)現(xiàn),我們之前設置的gravity被這個函數(shù)執(zhí)行之后覆蓋了。。
我忽然覺得估計是Google的大牛自測的時候?qū)懰雷兞亢谜{(diào)試,最后發(fā)布的時候忘記改了。。
問題定位到了,符合2個條件:
- popupWindow 在 show 的時候定義了 Gravity,不是 Gravity.START | Gravity.TOP。
- PopupWindow 調(diào)用了update。
解決方案
兩種方案
- 不調(diào)用 update 方法即可
- 重寫 update 方法
- 最簡單是 dismiss,再調(diào)show
- 反射方法 把gravity那一段去掉
我提供反射方法的辦法
但是我這個方法有缺陷的,反射的辦法會遇到Google Api如果把變量名改了,那就直接無效了。
但是要解決這種系統(tǒng)bug也只能做API適配了。
package cc.kinva.widget;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by Kinva on 16/9/23.
*/
public class FixedPopupWindow extends PopupWindow {
public FixedPopupWindow(Context context) {
super(context);
}
public FixedPopupWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedPopupWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public FixedPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public FixedPopupWindow(View contentView) {
super(contentView);
}
public FixedPopupWindow() {
super();
}
public FixedPopupWindow(int width, int height) {
super(width, height);
}
public FixedPopupWindow(View contentView, int width, int height, boolean focusable) {
super(contentView, width, height, focusable);
}
public FixedPopupWindow(View contentView, int width, int height) {
super(contentView, width, height);
}
@Override
public void update(int x, int y, int width, int height, boolean force) {
if (Build.VERSION.SDK_INT < 24) {
super.update(x, y, width, height, force);
return;
}
if (width >= 0) {
setParam("mLastWidth", width);
setWidth(width);
}
if (height >= 0) {
setParam("mLastHeight", height);
setHeight(height);
}
Object obj = getParam("mContentView");
View mContentView = null;
if (obj instanceof View) {
mContentView = (View) obj;
}
if (!isShowing() || mContentView == null) {
return;
}
obj = getParam("mDecorView");
View mDecorView = null;
if (obj instanceof View) {
mDecorView = (View) obj;
}
final WindowManager.LayoutParams p =
(WindowManager.LayoutParams) mDecorView.getLayoutParams();
boolean update = force;
obj = getParam("mWidthMode");
int mWidthMode = obj != null ? (Integer) obj : 0;
obj = getParam("mLastWidth");
int mLastWidth = obj != null ? (Integer) obj : 0;
final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth;
if (width != -1 && p.width != finalWidth) {
p.width = finalWidth;
setParam("mLastWidth", finalWidth);
update = true;
}
obj = getParam("mHeightMode");
int mHeightMode = obj != null ? (Integer) obj : 0;
obj = getParam("mLastHeight");
int mLastHeight = obj != null ? (Integer) obj : 0;
final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight;
if (height != -1 && p.height != finalHeight) {
p.height = finalHeight;
setParam("mLastHeight", finalHeight);
update = true;
}
if (p.x != x) {
p.x = x;
update = true;
}
if (p.y != y) {
p.y = y;
update = true;
}
obj = execMethod("computeAnimationResource");
final int newAnim = obj == null ? 0 : (Integer) obj;
if (newAnim != p.windowAnimations) {
p.windowAnimations = newAnim;
update = true;
}
obj = execMethod("computeFlags", new Class[]{int.class}, new Object[]{p.flags});
final int newFlags = obj == null ? 0 : (Integer) obj;
if (newFlags != p.flags) {
p.flags = newFlags;
update = true;
}
if (update) {
execMethod("setLayoutDirectionFromAnchor");
obj = getParam("mWindowManager");
WindowManager mWindowManager = obj instanceof WindowManager ? (WindowManager) obj : null;
if (mWindowManager != null) {
mWindowManager.updateViewLayout(mDecorView, p);
}
}
}
/**
* 反射獲取對象
* @param paramName
* @return
*/
private Object getParam(String paramName) {
if (TextUtils.isEmpty(paramName)) {
return null;
}
try {
Field field = PopupWindow.class.getDeclaredField(paramName);
field.setAccessible(true);
return field.get(this);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 反射賦值對象
* @param paramName
* @param obj
*/
private void setParam(String paramName, Object obj) {
if (TextUtils.isEmpty(paramName)) {
return;
}
try {
Field field = PopupWindow.class.getDeclaredField(paramName);
field.setAccessible(true);
field.set(this, obj);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 反射執(zhí)行方法
* @param methodName
* @param args
* @return
*/
private Object execMethod(String methodName, Class[] cls, Object[] args) {
if (TextUtils.isEmpty(methodName)) {
return null;
}
try {
Method method = getMethod(PopupWindow.class, methodName, cls);
method.setAccessible(true);
return method.invoke(this, args);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 利用遞歸找一個類的指定方法,如果找不到,去父親里面找直到最上層Object對象為止。
*
* @param clazz
* 目標類
* @param methodName
* 方法名
* @param classes
* 方法參數(shù)類型數(shù)組
* @return 方法對象
* @throws Exception
*/
private Method getMethod(Class clazz, String methodName,
final Class[] classes) throws Exception {
Method method = null;
try {
method = clazz.getDeclaredMethod(methodName, classes);
} catch (NoSuchMethodException e) {
try {
method = clazz.getMethod(methodName, classes);
} catch (NoSuchMethodException ex) {
if (clazz.getSuperclass() == null) {
return method;
} else {
method = getMethod(clazz.getSuperclass(), methodName,
classes);
}
}
}
return method;
}
}
歡迎圍觀 我的博客