【问题标题】:Cannot set PopupWindow background to transparent无法将 PopupWindow 背景设置为透明
【发布时间】:2012-12-19 16:54:05
【问题描述】:

我有以下代码:

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布局上,所有的relativelayouts都被赋予了透明色背景。

这里是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 设置为透明 - 我必须出于不同的原因对其进行更改)。

感谢任何帮助。

【问题讨论】:

  • 那么,问题出在哪里?我没有看到任何问题或问题。
  • @prolink007 不好意思,弹窗还是显示灰色背景,和view的父relativelayout一致。
  • 你能把你的xml文件贴出来,它会帮助解决这个问题。
  • @prolink007 正在发帖。

标签: android background android-mapview transparent


【解决方案1】:

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

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

【讨论】:

  • 这么简单...谢谢!
  • @MarkDelphi 欢迎您。很高兴它对你有用。
【解决方案2】:

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

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

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

【讨论】:

  • setBackgroundDrawable 将可绘制对象作为参数
  • 我不小心忘记了getResources.getDrawable()
【解决方案3】:

尝试将@android:style/Theme.Translucent 用于您的RelativeLayout,而不是更改background 颜色。

Here are some other suggestions.

【讨论】:

  • 不幸的是仍然无法正常工作。我已经尝试了建议的修复。它比原来做得更好:父布局是透明的。但是,图像背景现在不再显示,而是在其位置上放置了一些灰色的东西 - 但只是在图像背景上,而不是在整个布局上!
  • 你能画出想要的布局吗?表达你想要透明的部分和你不想要透明的部分?我很难理解您想要的布局。
  • 我设法修复了它。我会尽快在 SO 上发布结果,并附上解释。
  • 酷,期待看到你想要实现的目标。
  • 那么,解决方案是什么?
【解决方案4】:

使用 android:background="@null"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2018-07-03
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多