2012-12-03 115 views
8

enter image description here如何显示透明背景图像的弹出窗口?

好家伙,我想提出我在其中显示弹出窗口的背景图像,

背景图像我设置为弹出窗口是透明的图像 一个应用程序,但问题是,当显示在弹出窗口中的背景图片不能正常显示....

虽然它是显示图像周围的角落的黑条带的透明图像..

任何人可以帮助我?

PopupDemoActivity.java

包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

回答

16

Jayesh的背景内容,试试这个:您的弹出式视图,请的

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

感谢Vijju,它的工作.... :) – Jayesh

+0

这是一个完美的解决方案,因为它不禁止弹出解除外部点击 –

+0

你试过这个“popUp.setOutsideTouchable(true/false);” ? – Vijju

0

在你的XML您要定义弹出窗口的背景

的android:背景= “@颜色/ white_color”

尝试此处将背景图片,而不是运行时间。

机器人:背景= “@绘制/ order_back”

OR

移动弹出布局变更上述代码(popUp.showAtLocation(layout, Gravity.TOP | Gravity.RIGHT,0, 0);前)

+0

谢谢你的回答,但我需要弹出窗口的背景图像,而不是我为PopDemoActivity设置的布局作为setContentView();我不改变布局,但显示弹出窗口,我正在创建新的布局和设置背景......我不使用现有的背景..... – Jayesh

+0

@Jayesh你找出解决方案? – rana

+0

@rana:请参阅vijju的回答 – Jayesh

10

设置

popUp.setBackgroundDrawable(null); 

它应该做的工作。什么你得到的背景是某种类型的弹出窗口

+0

感谢您的回复,其工作.... – Jayesh

+0

其工作...但是当我在弹出窗口外单击时,它不会被解雇.... – OAEI

+0

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