pro*_*m85 7 android popupwindow android-popupwindow
这是我创建一个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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑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;
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的更新,我要求您展示该方法,因为我实际上在我的应用程序中使用相同类型的东西来检索颜色属性,但我们的方法有点不同.
这是我的:
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;
}
Run Code Online (Sandbox Code Playgroud)
即使我不能确定它确实是问题,但你的评论有问题.调用new ColorDrawable()期望颜色,而不是参考.我有时也在过去做过这个错误并且也有奇怪的颜色,因为系统试图生成带有参考ID的颜色.您是否尝试过像红色这样的真实颜色,看看您的方法是否真的有效?
我会用我的方法替换你的方法,因为它可以保证你检索一种颜色.
| 归档时间: |
|
| 查看次数: |
584 次 |
| 最近记录: |