Android 为什么 ProgressDialog 的背景没有设置为透明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21957263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:22:03  来源:igfitidea点击:

Why the background of ProgressDialog doesn't set to the transparent?

androidprogressdialogandroid-styles

提问by Martin

I want to set the back ground to the transparent , so I have set the following code in

我想将背景设置为透明,所以我设置了以下代码

styles.xml
<style name="dialog" parent="@android:style/Theme.Dialog">  
            <item name="android:windowFrame">@null</item>  
            <item name="android:windowIsFloating">true</item>  
            <item name="android:windowContentOverlay">@null</item>  
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>  
            <item name="android:windowBackground">@android:color/transparent</item>  
            <item name="android:windowNoTitle">true</item> 
        </style> 

And I have use the Progressdialoglike the following code in JAVA fileand in fragment.

ProgressdialogJAVA file和 中使用了类似下面的代码fragment

Activity activity = getActivity() ;
mProgressDialog = new ProgressDialog(activity,R.style.dialog) ;
mProgressDialog.setCancelable(false) ;
mProgressDialog.show() ;

But I get the progress like the following picture , and it doesn't has transparent background.

但是我得到如下图所示的进度,并且它没有透明背景。

enter image description here

在此处输入图片说明

Why the background doesn't change to the transparent ?

为什么背景没有变成透明的?

回答by M D

create custom MyThemein values\styles.xml

创建自定义MyThemevalues\styles.xml

<style name="MyTheme" parent="android:Theme.Holo.Dialog">
    <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
     <item name="android:backgroundDimEnabled">false</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">12sp</item>
</style>

And also add this CustomAlertDialogStylein values\styles.xml

并将其添加CustomAlertDialogStylevalues\styles.xml

 <style name="CustomAlertDialogStyle">
<item name="android:bottomBright">@android:color/transparent</item>
<item name="android:bottomDark">@android:color/transparent</item>
<item name="android:bottomMedium">@android:color/transparent</item>
<item name="android:centerBright">@android:color/transparent</item>
<item name="android:centerDark">@android:color/transparent</item>
<item name="android:centerMedium">@android:color/transparent</item>
<item name="android:fullBright">@android:color/transparent</item>
<item name="android:fullDark">@android:color/transparent</item>
<item name="android:topBright">@android:color/transparent</item>
<item name="android:topDark">@android:color/transparent</item>
</style>

And set ProgressDialoglike:

并设置ProgressDialog为:

 pd = new ProgressDialog(getActivity(),R.style.MyTheme);
 pd.setCancelable(false);
 pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
 pd.show();

回答by David Hackro

You can use this code,work fine in devices >= 19 (Kitkat)

您可以使用此代码,在设备 >= 19 (Kitkat) 中正常工作

progress = ProgressDialog.show(Splash.this, null, null, true);
            progress.setContentView(R.layout.elemento_progress_splash);
            progress.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

            //progress.show();

enter image description here

在此处输入图片说明

回答by AndyFaizan

Try this

尝试这个

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

EDIT:

编辑:

Try adding this to the layout xml

尝试将此添加到布局 xml

        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>

回答by user1513535

In my case, i just define at color.xmlthe variables

就我而言,我只是在color.xml 中定义变量

For light style: <color name="accent_material_light">#000000</color>

对于轻量级: <color name="accent_material_light">#000000</color>

For dark style: <color name="accent_material_dark">#000000</color>

对于深色风格: <color name="accent_material_dark">#000000</color>

These changes affect the whole system

这些变化影响整个系统

回答by Nanda Gopal

Just try this. It's working for me.

试试这个。它对我有用。

In styles.xml

styles.xml

<style name="TransparentProgressDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>

  </style>

In Activity/Fragment:

Activity/Fragment

ProgressDialog pDialog = new ProgressDialog(this, R.style.TransparentProgressDialog);