将Android中AlertDialog的背景设置为透明。

46
我想要显示一个带有透明背景的警告对话框。
我的警告对话框代码如下:
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();

我的背景图像包含圆角,但不幸的是弹出框出现时呈现为矩形白色背景。我该如何解决这个问题?

enter image description here

9个回答

105

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
谢谢您,黑带,它有效了。但现在显示黑色背景...我需要透明的。 - Neeha
2
你可以尝试使用Dialog而不是AlertDialog吗? - Blackbelt
1
谢谢你的回答,对我很有帮助。 - Sumit Pathak
1
谢谢。太棒了! - shariful islam
1
这对我有效,而ContextThemeWrapper则不行...奇怪但谢谢~ - Meow Cat 2012
显示剩余13条评论

46

另一种解决方法:

当使用 Alertdialog.builder 时,它不提供其中的 getwindow() 选项。 因此,我们可以这样做来使其工作:

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

你的 AlertDialog 的背景将会是透明的。


2
这就是确切的做法。干得好。谢谢。 - Alex K

9

不要这样做:

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

您可以尝试像这样做:

您可以尝试像这样做:

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

显示错误:类型AlertDialog.Builder的方法getWindow()未定义。 - Neeha
是的,抱歉。请看一下更新后的答案,现在应该可以工作了。 - zbr

4

调用这个方法,

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

就在之前

customDialog.show();

4

如果你只想从对话框布局中删除白色背景,那么你只需要添加:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

如果您还想去除对话框的窗口背景半透明颜色,那么您需要添加这两行代码:
dialog.getWindow().setDimAmount(0f);    
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

2
val dialog: AlertDialog = builder.create()
      dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.setView(view)
        dialog.show()```

1
请考虑为您的解决方案添加一些描述。在Stackoverflow上,仅有代码的答案是不被鼓励的。 - Amir Dora.

1
以下代码更改了背景布局。
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")));

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);
}

0

首先在 theme.xml 中设置自定义主题来为警告对话框设置主题

<style name="YourThemeName" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#CC8800</item>
    <item name="android:windowBackground">@android:color/transparent</item>

</style>

在这之后,在你的AlertDialog中设置这个主题

AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireActivity(),R.style.YourThemeName;
            alertDialog.setView(R.layout.your_custom_view);
            alertDialog.setCancelable(false);
            alertDialog.create();
            alertDialog.show();

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