将透明背景设置为android中的alertdialog

Nee*_*eha 29 android 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();
Run Code Online (Sandbox Code Playgroud)

我的背景图片包含圆角.但不幸的是,pop出现了矩形白色背景.任何身体PLZ建议我的想法.谢谢你提前.

Bla*_*elt 67

定义styles.xml文件中的后续内容

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并将其作为参数传递给AlertDialog构造函数

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);
Run Code Online (Sandbox Code Playgroud)

或者以编程方式,通过Dialog您可以调用的实例

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
Run Code Online (Sandbox Code Playgroud)

  • 它没有blackbelt工作.但现在显示黑色背景..我需要透明. (3认同)
  • 您可以尝试使用 Dialog 而不是 AlertDialog 吗? (2认同)

Sag*_*hah 30

还有一个解决方案

使用时Alertdialog.builder- 它没有给出getwindow()选项.所以我们可以让它像这样工作:

AlertDialog dialog = builderScan.create();
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.show();
Run Code Online (Sandbox Code Playgroud)

你的背景AlertDialog将是transperent.

  • 这是做到这一点的确切方法。一路走好。谢谢你。 (2认同)

zbr*_*zbr 5

代替这个:

imageDialog.create();
imageDialog.show();
Run Code Online (Sandbox Code Playgroud)

您可以尝试执行以下操作:

AlertDialog imageDialogAlert = imageDialog.create();
imageDialogAlert.show();
imageDialogAlert.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)