这是我创建
如何避免这种情况?例如在模拟器上只有安卓6有时才会发生... 通常,
PopupWindow
的方法:private static PopupWindow createPopup(FragmentActivity activity, View view)
{
PopupWindow popup = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable(Tools.getThemeReference(activity, R.attr.main_background_color)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
popup.setElevation(Tools.convertDpToPixel(8, activity));
PopupWindowCompat.setOverlapAnchor(popup, true);
return popup;
}
main_background_color
是一个实心颜色,根据主题而定,通常为白色或黑色。有时会出现以下情况:
![enter image description here](https://istack.dev59.com/A835y.webp)
PopupWindow
背景的工作都正常...
编辑
此外,这是我的getThemeReference
方法:public static int getThemeReference(Context context, int attribute)
{
TypedValue typeValue = new TypedValue();
context.getTheme().resolveAttribute(attribute, typeValue, false);
if (typeValue.type == TypedValue.TYPE_REFERENCE)
{
int ref = typeValue.data;
return ref;
}
else
{
return -1;
}
}
编辑2 - 这可能解决问题:使用getThemeColor
而不是getThemeReference
public static int getThemeColor(Context context, int attribute)
{
TypedValue typeValue = new TypedValue();
context.getTheme().resolveAttribute(attribute, typeValue, true);
if (typeValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typeValue.type <= TypedValue.TYPE_LAST_COLOR_INT)
{
int color = typeValue.data;
return color;
}
else
{
return -1;
}
}
getThemeReference
方法。 - Yoann Hercouet