更改ProgressDialog的背景

aha*_*aha 26 android android-layout android-dialog

我想改变一个的背景ProgressDialog.我在网上搜索并找到了各种建议(比如如何从Dialog中删除边框?),但是我无法替换它的实际背景ProgressDialog.相反,我在对话框后面得到另一个背景(黄色):

样式对话框

我的风格:

<style name="StyledDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@drawable/panel_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)

启动以下代码ProgressDialog:

ProgressDialog dialog = new ProgressDialog(this, R.style.StyledDialog);
dialog.setTitle("The title");
dialog.setMessage("The message.");
dialog.show();
Run Code Online (Sandbox Code Playgroud)

drawable与SDK中包含的9个补丁相同,我只是改为颜色.我非常感谢一些暗示我做错了什么.

aha*_*aha 64

Aleks G的评论(问题下方)指向了正确的方向.对话框的外观由单独的样式(android:alertDialogStyle)定义.但是不能直接将风格应用于a ProgressDialog.现在,我如何获得黄色背景?

第1步:定义继承自的主题Theme.Dialog:

<style name="MyTheme" parent="@android:style/Theme.Dialog">
    <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
    <item name="android:textColorPrimary">#000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在那里,你可以定义整个窗口的背景颜色(问题中的黄色),字体颜色等等.真正重要的是定义android:alertDialogStyle.此样式控制问题中黑色区域的外观.

第2步:定义CustomAlertDialogStyle:

<style name="CustomAlertDialogStyle">
    <item name="android:bottomBright">@color/yellow</item>
    <item name="android:bottomDark">@color/yellow</item>
    <item name="android:bottomMedium">@color/yellow</item>
    <item name="android:centerBright">@color/yellow</item>
    <item name="android:centerDark">@color/yellow</item>
    <item name="android:centerMedium">@color/yellow</item>
    <item name="android:fullBright">@color/yellow</item>
    <item name="android:fullDark">@color/yellow</item>
    <item name="android:topBright">@color/yellow</item>
    <item name="android:topDark">@color/yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这会将问题中的黑色区域设置为黄色.

第3步:应用MyThemeProgressDialog,没有 CustomAlertDialogStyle:

ProgressDialog dialog = new ProgressDialog(this, R.style.MyTheme);
Run Code Online (Sandbox Code Playgroud)

这是结果:

Styled ProgressDialog

相同的过程适用于AlertDialog(父类ProgressDialog).

  • @ lyc001你需要dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 或<item name ="android:windowBackground"> @ android:color/transparent </ item> (3认同)