【发布时间】:2016-06-27 09:23:06
【问题描述】:
这是我创建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 是纯色,白色或黑色,取决于主题。有时会发生以下情况:
我怎样才能避免这种情况?它发生在具有 android 6 SOMETIMES 的模拟器中,例如...通常,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方法。 -
完成。尽管如此,我不相信问题的原因在于那里,因为这意味着这个问题总是发生,但它很少发生(直到现在,我只在 android 6 上看到它)
标签: android popupwindow android-popupwindow