【问题标题】:PopupWindow background sometimes gets transparent and purplePopupWindow 背景有时会变得透明和紫色
【发布时间】: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


【解决方案1】:

感谢您的更新,我要求您展示该方法,因为我实际上在我的应用程序中使用相同的东西来检索颜色属性,但我们的方法有点不同。

这是我的:

public static int getThemeColor(Context context, int attributeId) {
    TypedValue typedValue = new TypedValue();

    TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] { attributeId });
    int color = a.getColor(0, 0);

    a.recycle();

    return color;
}

尽管我不能确定确实是问题所在,但您的 cmets 有问题。调用 new ColorDrawable() 需要颜色,而不是参考。我过去有时也犯过这个错误,也得到了奇怪的颜色,因为系统试图生成带有参考 ID 的颜色。您是否尝试过像红色这样的真实颜色,看看您的方法是否真的有效?

无论如何我都会用我的方法替换你的方法,因为它保证你可以检索颜色。

【讨论】:

  • 所以你认为(我可能做错了,这是真的),相同的设备和系统,用我的方法创建不同的颜色?运行我的应用程序时会发生这种情况。你打开弹窗,没关系,你旋转设备,重新打开弹窗,它是紫色的(它的颜色变化莫测,大多数时候颜色是正确的)
  • 顺便说一句,我的R.attr.main_background_color 是一个参考...这不是颜色...我有第二种方法,与您的名称相同;-)。它也应该可以工作,我也刚刚在我的问题中发布了它......这可能真的是因为我传递了一个颜色参考而不是一个完全解析的颜色......
  • @prom85 正如我所说,不确定它是否会有所作为,但值得一试。在您提供的其余代码中,我没有看到任何其他可能与此问题相关的内容。
  • 至少,ColorDrawable 期望有颜色且没有引用的提示仍然是正确的,所以这可能是问题所在......我会尝试看看,如果问题消失了
  • 直到现在,这似乎真的解决了问题
猜你喜欢
  • 2019-09-29
  • 2014-11-11
  • 1970-01-01
  • 2013-11-25
  • 2015-01-28
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多