Android如何去除PopupWindow的背景?

6

我使用PopupWindow类创建自定义弹出窗口。

但是,当我在主界面上添加layout_margin(例如15dp)时,会出现透明的灰色背景。

如何去掉透明背景?

请参见图片。

enter image description here

编辑 这是我的代码:

    window = new PopupWindow(customTool.getContext());
    window.setWidth(WindowManager.LayoutParams.FILL_PARENT);
    window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    window.setTouchable(true);
    window.setFocusable(true);
    window.setOutsideTouchable(true);
    window.setAnimationStyle(R.style.Animations_PopDownMenu_Left);
window.setContentView(customTool);
    window.showAtLocation(customTool, Gravity.NO_GRAVITY, 0, 100);

你在 popupWindow 对象上调用了 setBackgroundDrawable(Drawable background) 方法吗? - Gopal
不,我不使用setBackgroundDrawable,我会编辑问题并附上我的代码。 - Jovan
是的,你需要调用setBackgroundDrawable方法,可以传入null或者BitmapDrawable对象。 - Gopal
1
在xml中,我使用android:background="#0ff000",这个很好地工作了。 - Jovan
很酷,顺带一提:android:background、setBackgroundResource和setBackgroundDrawable都是做同样的事情。如果你想看看它是如何实现的,这里有一个链接,可以搜索setBackgroundResource。 - Gopal
1个回答

9
- 尝试在您的弹出对话框上设置yourDiag.setBackgroundDrawable(null);

2
当我使用 setBackgroundDrawable(null); 时,透明背景消失了,但是 setOutsideTouchable 不起作用。我实现了 setTouchInterceptor,但是当我设置 setBackgroundDrawable(null); 时不起作用。有什么建议吗? - Jovan
3
我使用new BitmapDrawable()代替setBackgroundDrawable(null); - Jovan
Jovan 是正确的。这看起来像是 PopupWindow 中的一个 bug - 将背景 drawable 设置为 null 会导致 OnTouchListener 失效,但使用一个新的、空的 BitmapDrawable() 就可以解决问题。 - Jerry Brady
或者,可以使用以下代码替代 setBackgroundDrawable(null)popupWindow.setBackgroundDrawable(new ColorDrawable(ctx.getResources.getColor(android.R.color.transparent))) - George Pligoropoulos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接