2013-04-24 176 views
13

enter image description here设置在android中透明背景alertdialog

我想显示透明背景的警报对话框。 我的警告对话框的代码是:

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this); 
LayoutInflater inflater = (LayoutInflater)SubProducts.this.getSystemService(LAYOUT_INFLATER_SERVICE); 

View layout = inflater.inflate(R.layout.cust_toast_layout,(ViewGroup)findViewById(R.id.linearLayout2)); 

ImageView image = (ImageView)layout.findViewById(R.id.imageView1); 
image.setPadding(0, 20, 0, 0); 
imgLoader.DisplayImage(image_url, loader, image); 


TextView tprice=(TextView)layout.findViewById(R.id.pricetext); 
tprice.setText("$ "+pricedouble); 

TextView tvdprh=(TextView)layout.findViewById(R.id.textView1); 

tvdprh.setText(prohd); 




WebView wv=(WebView)layout.findViewById(R.id.webview); 



Spanned sub=Html.fromHtml(descp); 
String s = "<html><head><style type='text/css' >@font-face {font-family:'myfont';src: url('file:///android_asset/fonts/ABeeZee-Regular.ttf');}body {margin:0px;color:000000;font-family: myfont;" 
     + "text-align: justify;}</style></head><body>" 
     + sub 
     + "</body></html>"; 

wv.loadDataWithBaseURL("", s, "text/html", "utf-8", null); 
wv.setVerticalScrollBarEnabled(true); 
wv.setBackgroundColor(Color.TRANSPARENT); 
wv.setPadding(5, 25, 5, 0); 


ImageView imgcartl=(ImageView)layout.findViewById(R.id.imageView2); 
imgcartl.setBackgroundResource(R.drawable.cartlines); 

ImageView brobutton=(ImageView)layout.findViewById(R.id.imageView3); 
brobutton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 



     Intent intentlabl = new Intent(getBaseContext(), Label.class); 
Bundle b=new Bundle(); 
b.putString("url", image_urlpdf); 
b.putBoolean("isDialog", true); 
intentlabl.putExtras(b); 
startActivity(intentlabl); 

} 
     }); 

ImageView shobutton=(ImageView)layout.findViewById(R.id.imageView4); 

shobutton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
     // TODO Auto-generated method stub 
//intent code 
     } 
     }); 

ImageView addbutton=(ImageView)layout.findViewById(R.id.imageView5); 
addbutton.setBackgroundResource(R.drawable.addicon); 
addbutton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
     // TODO Auto-generated method stub 

     passingid.add(prodid); 

Product prodobj=new Product(); 
prodobj.setId(passingid); 


new LongRunningGetIO4().execute(pricedouble, prodid); 
} 
     }); 


imageDialog.setView(layout); 


imageDialog.create(); 
imageDialog.show(); 

我的背景图片包含圆润corners.But不幸的是,流行与长方形的白色background.Any体PLZ出现建议我提前idea.Thanks。

回答

4

取而代之的是:

你可以尝试做这样的事情:

AlertDialog imageDialogAlert = imageDialog.create(); 
imageDialogAlert.show(); 
imageDialogAlert.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
+0

它显示错误:类型AlertDialog.Builder的方法getWindow()未定义 – Neeha 2013-04-24 09:59:34

+0

是的,抱歉。请看看最新的答案,它现在应该可以工作。 – zbr 2013-04-24 11:34:10

44

定义在styles.xml文件

<style name="CustomDialog" parent="android:Theme.Dialog"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 

接下来并把它作为参数传递给AlertDialog构造函数

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog); 

或以编程方式,通过Dialog例如,您可以拨打

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)) 
+3

thanq blackbelt它的工作。但现在显示黑色背景..我需要透明。 – Neeha 2013-04-24 09:43:01

+0

这很奇怪。 R.layout.cust_toast_layout有黑底色吗? – Blackbelt 2013-04-24 09:47:25

+0

是的,我在我的问题中添加了警报对话框的快照.Plz看一看。 – Neeha 2013-04-24 10:09:27

1
protected AlertDialog(Context context) { 
    this(context, com.android.internal.R.style.Theme_Translucent_NoTitleBar_Fullscreen); 
} 

public Builder(Context context) { 
    this(context, com.android.internal.R.style.Theme_Translucent_NoTitleBar_Fullscreen); 
} 
12

还有一个解决办法:

当使用Alertdialog.builder - 它不给它getwindow()选项。 因此,我们可以把它像这样工作的:

AlertDialog dialog = builderScan.create(); 
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
      dialog.show(); 

和你的AlertDialog背景将是透明单位。

+1

这是完成此操作的确切方法。路要走。谢谢。 – 2014-12-22 20:37:39

1

调用此,

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

之前

customDialog.show(); 
0

下面的代码更改背景布局。

LayoutInflater inflater = MainActivity.this.getLayoutInflater(); 

View layout = inflater.inflate(R.layout.your_layout, null); 

final AlertDialog alertDio = new AlertDialog.Builder(MainActivity.this) 
         .setView(layout) 
         .show(); 

alertDio.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.parseColor("#801b5e20")));