弹出窗口打开android后变暗背景

Bil*_*doy 5 android android-studio

我想在弹出窗口打开后使背景变暗,并在关闭后恢复之前的所有内容。我已经搜索过谷歌并看到了一些教程。但我无法理解这怎么可能。这是我的 Java 代码:

public void displayPopupWindow() {
    View layout = getLayoutInflater().inflate(R.layout.popup,null);
    final PopupWindow popup = new PopupWindow(this);
    popup.setContentView(layout);
    popup.setOutsideTouchable(true);
    popup.setFocusable(true);
    popup.setWidth(400);
    popup.setHeight(580);
    popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
    popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    Button button = (Button) layout.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View layout) {
            popup.dismiss();
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

这是我的弹出 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary"
android:id="@+id/popup"
>
Various views and button
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我现在该怎么办?

Apo*_*tra -1

你可以试试这个

代替

popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)

在你的代码中使用这个

popup.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
Run Code Online (Sandbox Code Playgroud)

您可以根据选择更改任何背景。