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" />

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

任何帮助表示赞赏。

4

4 回答 4

23

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

popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
于 2015-02-02T11:11:43.843 回答
6

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

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

这样,弹出窗口仍然可以关闭,如果您尝试使用 null,则情况并非如此。

于 2014-03-19T02:52:29.560 回答
0

使用 android:background="@null"

于 2013-03-23T15:04:59.477 回答
0

尝试使用@android:style/Theme.Translucentfor yourRelativeLayout而不是更改background颜色。

这里有一些其他的建议。

于 2012-12-19T17:56:01.617 回答