如何显示带有透明背景图像的弹出窗口?

16

enter image description here

大家好,我正在制作一个应用程序,在其中显示带有背景图片的弹出窗口。

我将背景图片设置为透明图像,但问题是当弹出窗口被显示时,背景图片无法正确显示......

尽管它是一张透明图片,但它显示了图像边角周围的黑色条纹。

有人能帮我吗?

PopupDemoActivity.java

package com.demo.popupwindow.;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;  
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

public class PopupDemoActivity extends Activity {

Button searchMenu, viewOrder;

PopupWindow popUp;
LayoutParams params;
FrameLayout layout;
// LinearLayout layout;
boolean click = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popdemodemo);

    searchMenu = (Button) findViewById(R.id.menu);
    viewOrder = (Button) findViewById(R.id.order);
    popUp = new PopupWindow(this);

    // layout = new LinearLayout(this);
    layout = new FrameLayout(this);


    viewOrder.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if (click) {
                popUp.showAtLocation(layout, Gravity.TOP | Gravity.RIGHT,
                        0, 0);
                popUp.update(30, 75, 500, 400);
                click = false;
            } else {
                popUp.dismiss();
                click = true;
            }

        }
    });

    // popUp.setContentView(layout);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    layout.setBackgroundResource(R.drawable.order_back);
    // layout.setBackgroundColor(Color.TRANSPARENT);
    popUp.setContentView(layout);
}

}

popupdemo.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/white_color"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/header_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/menu"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="Search Menu"
        android:textColor="@color/white_color"
        android:textSize="25sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/order"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:text="View Order"
        android:textColor="@color/white_color"
        android:textSize="25sp"
        android:textStyle="bold" />
</RelativeLayout>


1
请发布您的弹出视图的布局文件。 - Wenhui
3个回答

31

Jayesh,试试这个:

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

这是完美的解决方案,因为它不会阻止弹出窗口在外部单击时消失。 - Muhammed Refaat
你尝试过这个 "popUp.setOutsideTouchable(true/false);" 吗? - Vijju

11

设置

popUp.setBackgroundDrawable(null);

它应该能完成工作。你在后台得到的是弹出窗口的某种背景内容。


你需要做的是创建一个透明的PNG文件(如clear.png),并将其放置在你的drawable文件夹中,然后调用popUp.setBackgroundDrawable(R.drawable.clear);。 - pt123

1

在您的XML中,您正在定义弹出窗口的背景为

android:background="@color/white_color"

尝试在此处应用背景图像,而不是在运行时。

android:background="@drawable/order_back"

或者

将弹出窗口布局更改代码移到上面(在 popUp.showAtLocation(layout, Gravity.TOP | Gravity.RIGHT,0, 0);之前)


谢谢您的回答,但我需要弹出窗口的背景图像,而不是我设置为setContentView()的PopDemoActivity布局的背景图像;我没有更改布局,只是为了显示弹出窗口,我正在创建新布局并设置背景... 我没有使用现有的背景..... - Jayesh
@Jayesh,你找到解决方案了吗? - rana
@rana:请看vijju的回答。 - Jayesh

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