更改弹出对话框的背景颜色

Ric*_*ick 40 android background-color android-alertdialog

我编写了一个显示弹出对话框的android代码,但是我想将背景颜色从黑色更改为白色,然后是写入的颜色.

这是对话框的代码:

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

    if (!welcomeScreenShown) {


        String whatsNewText = getResources().getString(R.string.Text);
        new AlertDialog.Builder(this).setMessage(whatsNewText).setPositiveButton(
                R.string.ok, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }
Run Code Online (Sandbox Code Playgroud)

tir*_*r38 77

要扩展@DaneWhite的答案,您不必依赖内置主题.您可以轻松提供自己的风格:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@color/myColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后将其应用于Builder构造函数:

AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
        ...
        .create();
Run Code Online (Sandbox Code Playgroud)

无论您是使用android.support.v7.app.AlertDialog还是使用,这都应该有效android.app.AlertDialog

这也比@ DummyData的答案更好,因为你没有调整对话框的大小.如果设置窗口的背景可绘制,则覆盖一些现有的尺寸信息并获得非标准宽度的对话框.

如果你在主题上设置背景并在对话框上设置主题,你最终会得到一个对话框,它会按照你想要的颜色,但仍然是正确的宽度.

  • 实际上,“background”和“windowBackground”都从对话框窗口中删除了圆角。如果你想设置颜色,你应该使用“colorBackground”属性。 (3认同)
  • 如何更改文本和按钮颜色? (2认同)
  • 您应该改用`android:windowBackground` 主题属性。`android:background` 将改变每个 `View` 的背景颜色。 (2认同)

Dan*_*ite 58

如果您只想要一个浅色主题而不是特定颜色,那么您可以将主题ID传递给AlertDialog.Builder构造函数.

AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT)...
Run Code Online (Sandbox Code Playgroud)

要么

AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)...
Run Code Online (Sandbox Code Playgroud)

  • 它说现在已经弃用了吗?现在有任何建议吗? (8认同)
  • @Dinesh如果您使用的是Android Studio,它将直接告诉您替换:*已弃用使用android.R.style.Theme_Material_Light_Dialog_Alert* (5认同)

Dum*_*ata 31

幸得苏希尔

像往常一样创建AlertDialog:

AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
Dialog dialog = dialog.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)

对话框上调用show(),设置背景颜色如下:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
Run Code Online (Sandbox Code Playgroud)

  • 小心.虽然这将设置背景颜色,但您将替换包含宽度尺寸的先前背景可绘制.生成的对话框可能与未更新背景可绘制对象的大小不同. (3认同)

Gab*_*tti 13

使用材料组件库,您可以只使用默认值MaterialAlertDialogBuilder

    new MaterialAlertDialogBuilder(AlertDialogActivity.this,
        R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background)
        .setTitle("Dialog")
        .setMessage("Message...  ....")
        .setPositiveButton("Ok", /* listener = */ null)
        .show();
Run Code Online (Sandbox Code Playgroud)

主题叠加在哪里ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background

  <!-- Alert Dialog -->
  <style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog_Background" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <!-- Background Color-->
    <item name="android:background">@color/.....</item>
    <!-- Text Color for title and message -->
    <item name="colorOnSurface">@color/......</item>
    <!-- Style for positive button -->
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <!-- Style for negative button -->
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
  </style>

  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
    <!-- text color for the button -->
    <item name="android:textColor">@color/.....</item>
    <!-- Background tint for the button -->
    <item name="backgroundTint">@color/primaryDarkColor</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


kik*_*283 11

对于任何myDialog调用的对话框,调用后myDialog.show();可以调用:

myDialog.getWindow().getDecorView().getBackground().setColorFilter(new LightingColorFilter(0xFF000000, CUSTOM_COLOR));
Run Code Online (Sandbox Code Playgroud)

其中CUSTOM_COLOR是8位十六进制格式,例如 0xFF303030.这里FF是alpha值,其余是十六进制的颜色值.

  • +1,比使用`setBackgroundDrawableResource` 更好的解决方案,因为您不必担心大小或形状组件。你也可以使用 `... .setColorFilter(int color, Mode mode)`,其中 `color` 将是你可以从 `getColor(int resId)` 之类获得的颜色代码 int,对于 `mode` 你可以使用`Mode.SRC` 来完全覆盖之前的颜色。 (2认同)

Mak*_*nov 5

要更改应用程序中所有对话框和弹出窗口的背景颜色,请使用colorBackgroundFloating属性。

<style name="MyApplicationTheme" parent="@style/Theme.AppCompat.NoActionBar">
...
<item name="colorBackgroundFloating">
    @color/background</item>
<item name="android:colorBackgroundFloating" tools:targetApi="23">
    @color/background</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)

说明文件:

colorBackgroundFloating