无法将PopupWindow背景设置为透明。

10

我有如下代码:

View child = getLayoutInflater().inflate(R.layout.contextual_menu_lp_activity, null)   
child.setBackgroundColor(getResources().getColor(R.color.transparent));
child.setBackgroundDrawable(new BitmapDrawable());
popup = new PopupWindow(MapViewActivity.this);
popup.setContentView(child);
popup.showAtLocation(MapViewActivity.mapView, Gravity.CENTER, 10,10);                           
popup.setBackgroundDrawable(new BitmapDrawable());                          
child.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                    popup.update(50, 50,child.getMeasuredWidth(), child.getMeasuredHeight());

正如您所看到的,我设置了视图,并拼命尝试使其背景透明。

在xml布局中,所有相对布局都被赋予了透明的背景颜色。

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/transparent" >

<RelativeLayout
    android:id="@+id/ivDialogPopup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:paddingBottom="40dp"
    android:background="@drawable/new_pop_up_bk" >

<Button
    android:id="@+id/btLivePolice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btSaveParkingLocation"
    android:layout_marginRight="4dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/live_police_button_popup" />

<Button
    android:id="@+id/btSaveParkingLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"
    android:background="@drawable/parking_location_button" />

<Button
    android:id="@+id/btGetDirections"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginTop="8dp"
    android:layout_toRightOf="@+id/btSaveParkingLocation"
    android:background="@drawable/directions_button" />

然而,视图的父布局(一个相对布局)仍具有灰色背景。(以前,我使用的是普通的activity类,在清单中我有一个透明主题 - 最重要的部分是将android:windowBackground设置为透明 - 我必须出于不同的原因进行更改)。

任何帮助都将不胜感激。


那么,问题是什么?我没有看到任何问题或困难陈述。 - prolink007
@prolink007 抱歉,弹出窗口仍然显示灰色背景,与视图的父相对布局一致。 - Radu
你能贴出你的xml文件吗?这将有助于解决这个问题。 - prolink007
@prolink007 正在发布帖子。 - Radu
4个回答

24

您也可以像下面这样设置透明背景。对我有效。

popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

1
如此简单...谢谢! - Mark Delphi
1
@MarkDelphi 没问题,很高兴它对你有用。 - hims_3009

7

您需要做的是创建一个透明的png文件(例如clear.png),并将其放置在drawable文件夹中,然后调用

yourPopUp.setBackgroundDrawable(getResources().getDrawable(R.drawable.clear));

如果尝试使用 null,那么弹出窗口仍然可以被关闭,但这并不总是起作用。


setBackgroundDrawable接受一个drawable作为参数。 - nikhil
我不小心忘记了getResources.getDrawable()。 - pt123

0

使用 android:background="@null"


0

非常遗憾,仍然无法正常工作。我已经尝试了建议的修复方法。它比原来的好:父布局是透明的。然而,图像背景现在不再显示,取而代之的是一些灰色的东西 - 但只覆盖在图像背景上,而不是整个布局上! - Radu
你能否画出所需的布局,并表达哪些部分需要透明,哪些部分不需要透明?我很难理解你想要的布局。 - prolink007
我已经修复了它。我会尽快在SO上发布结果,并附带解释。 - Radu
1
很酷,期待看到你想要实现什么。 - prolink007
1
那么,什么是解决方案? - LKallipo
@LKallipo 我一直没抽出时间,但我会在今天或明天完成。 - Radu

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