[英]set Transparent background to alertdialog in android
I want to show alert dialog with transparent background.我想显示具有透明背景的警报对话框。 My code of alert dialog is:我的警报对话框代码是:
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();
My Background image contains rounded corners.But unfortunately,pop is appearing with rectangle white background.Any body plz suggest me with idea.Thanks in Advance.我的背景图像包含圆角。但不幸的是,pop 出现在矩形白色背景上。任何身体请给我建议。提前谢谢。
define what follows in the styles.xml
file在styles.xml
文件中定义以下内容
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
and pass it as argument to the AlertDialog
constructor并将其作为参数传递给AlertDialog
构造函数
AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);
Or programmatically, through the Dialog
instance you can call或者以编程方式,您可以通过Dialog
实例调用
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
One more solution:另一种解决方案:
When using Alertdialog.builder
- Its not giving getwindow()
option in it.使用Alertdialog.builder
- 它没有提供getwindow()
选项。 So we can make it work like this:所以我们可以让它像这样工作:
AlertDialog dialog = builderScan.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
And your Background of AlertDialog
will be transperent.您的AlertDialog
背景将是透明的。
Instead of this:取而代之的是:
imageDialog.create();
imageDialog.show();
you could try doing something like this:你可以尝试做这样的事情:
AlertDialog imageDialogAlert = imageDialog.create();
imageDialogAlert.show();
imageDialogAlert.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Call this,叫这个,
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
just before就在之前
customDialog.show();
If you just want to remove white background from the dialog layout then you just add:如果您只想从对话框布局中删除白色背景,那么您只需添加:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
If you also want remove window background translucent colour for the dialog then you need to add both lines:如果您还想删除对话框的窗口背景半透明颜色,则需要添加两行:
dialog.getWindow().setDimAmount(0f);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
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);
}
The code below changes the background layout.下面的代码更改了背景布局。
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")));
val dialog: AlertDialog = builder.create()
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setView(view)
dialog.show()```
First set a custom theme for alert dialog in theme.xml首先在 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>
after this set this theme on your AlertDialog在此之后在您的 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.