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

Android 更改弹出对话框的背景色,android,android-alertdialog,background-color,Android,Android Alertdialog,Background Color,我编写了显示弹出对话框的android代码,但我想将背景颜色从黑色更改为白色,然后更改文字的颜色 这是对话框的代码: mPrefs = PreferenceManager.getDefaultSharedPreferences(this); Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); if (!welcomeScreenShown) { Str

我编写了显示弹出对话框的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
    }

在警报对话框生成器上使用
setInverseBackgroundForced(true)
反转背景。

您可以创建自定义警报对话框并使用xml布局。在布局中,可以设置背景颜色和文本颜色

大概是这样的:

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater)ActivityName.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout,(ViewGroup)findViewById(R.id.layout_root));
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
AlertDialog.Builder(requireContext(), R.style.MyDialogStyle)

如果您只需要一个灯光主题,而对特定的颜色不太在意,那么可以将主题id传递给AlertDialog.Builder构造函数

AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT)...

功劳归

像往常一样创建AlertDialog:

AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
Dialog dialog = dialog.create();
dialog.show();
在对话框上调用show()后,如下设置背景色:

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater)ActivityName.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout,(ViewGroup)findViewById(R.id.layout_root));
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
AlertDialog.Builder(requireContext(), R.style.MyDialogStyle)

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

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@color/myColor</item>
</style>
科特林:

var alertDialog = AlertDialog.Builder(context, R.style.MyDialogTheme)
        ...
        .create()
无论您使用的是
android.support.v7.app.AlertDialog
还是
android.app.AlertDialog

这也比@DummyData的答案更有效,因为您不需要调整对话框的大小。如果将窗口的背景设置为可绘制,则会覆盖一些现有标注信息,并得到一个非标准宽度的对话框


如果您在主题上设置背景,并在对话框上设置主题,您将得到一个对话框,该对话框的颜色是您想要的,但宽度仍然正确。

对于任何名为
myDialog
的对话框,在调用
myDialog.show()之后您可以调用:

myDialog.getWindow().getDecorView().getBackground().setColorFilter(new LightingColorFilter(0xFF000000, CUSTOM_COLOR));

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

仅更新导入

import android.app.AlertDialog;


它将被修复。

为了更改对话框按钮和背景颜色,您需要扩展对话框主题,例如:

<style name="MyDialogStyle" parent="android:Theme.Material.Light.Dialog.NoActionBar">
    <item name="android:buttonBarButtonStyle">@style/MyButtonsStyle</item>
    <item name="android:colorBackground">@color/white</item>
</style>

<style name="MyButtonsStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/light.blue</item>
</style>
如果要更改对话框中文本的颜色,可以将自定义视图传递给此生成器:

AlertDialog.Builder.setView(View)


要更改应用程序中所有对话框和弹出窗口的背景色,请使用
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>

...
@颜色/背景
@颜色/背景
...
文件:


使用材质组件库可以使用默认值:

其中主题叠加<代码>主题覆盖材料组件材料标签背景为:

  <!-- 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>

@颜色/。。。。。
@颜色/。。。。。。
@样式/正按钮样式
@样式/负片按钮样式
@颜色/。。。。。
@颜色/原色

但是我可以添加一行代码来更改颜色而不做所有这些事情吗?也许你可以试试这个:getWindow().setBackgroundDrawableResource(android.R.color.white);我希望您在正确的位置编写它,并且只获得alertDialog窗口..如果您添加了_DIALOG.getWindow().setBackgroundDrawableResource(android.R.color.white);它会对你有用…它说现在不推荐了?现在有什么建议吗?@Dinesh如果您使用的是Android Studio,它会直接告诉您替换:不推荐使用Android.R.style.Theme\u Material\u Light\u Dialog\u小心。虽然这将设置背景颜色,但您将替换以前的背景可绘制,其中包含宽度标注。如果您没有更新后台绘图,您生成的对话框的大小可能会不同。我希望我们可以通过主题资源使用一些样式属性来完成。我认为必须有这样的可能性,设置的不是颜色,而是任何其他可绘制的背景资源。如何更改文本和按钮的颜色?您应该使用
android:windowBackground
主题属性
android:background
将更改每个
视图的背景颜色。实际上,
background
windowBackground
都会从对话框窗口中删除圆角。如果要设置颜色,应该使用
colorBackground
属性来代替。+1,这比使用
setBackgroundDrawableResource
要好得多,因为您不必担心组件的大小或形状。您也可以使用
。setColorFilter(int-color,Mode模式)
,其中
color
将是您可以从
getColor(int-resId)
中获得的颜色代码int,对于
mode
您可以使用
mode.SRC
完全覆盖以前的颜色。这对我不起作用。我正在查看容器颜色元素没有属性的位置。我试图编辑colorSurface属性,但没有得到任何结果。谢谢你的回答。
    new MaterialAlertDialogBuilder(AlertDialogActivity.this,
        R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background)
        .setTitle("Dialog")
        .setMessage("Message...  ....")
        .setPositiveButton("Ok", /* listener = */ null)
        .show();
  <!-- 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>