【问题标题】:Change the background color of a pop-up dialog更改弹出对话框的背景颜色
【发布时间】:2013-08-23 04:55:00
【问题描述】:

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

【问题讨论】:

    标签: android android-alertdialog background-color


    【解决方案1】:

    通过材料组件库,您可以使用默认的MaterialAlertDialogBuilder

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

    主题覆盖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>
    

    【讨论】:

    【解决方案2】:

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

    文档:

    colorBackgroundFloating

    【讨论】:

      【解决方案3】:

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

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

      然后在 Builder 构造函数中应用它:

      Java:

      AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
              ...
              .create();
      

      科特林:

      var alertDialog = AlertDialog.Builder(context, R.style.MyDialogTheme)
              ...
              .create()
      

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

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

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

      【讨论】:

      • 如何更改文本和按钮颜色?
      • 您应该改用android:windowBackground 主题属性。 android:background 会改变每个View 的背景颜色。
      • 实际上,backgroundwindowBackground 都从对话窗口中删除了圆角。如果要设置颜色,则应使用colorBackground 属性。
      • 此解决方案还在对话框中将文本颜色更改为更暗
      【解决方案4】:

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

      <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(requireContext(), R.style.MyDialogStyle)
      

      如果你想改变对话框内文本的颜色,你可以给这个Builder传递一个自定义视图:

      AlertDialog.Builder.setView(View)
      

      AlertDialog.Builder.setView(@LayoutResource int)
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          仅更新您的导入。

          import android.app.AlertDialog;
          

          import android.support.v7.app.AlertDialog;
          

          它将被修复。

          【讨论】:

            【解决方案7】:

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

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

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

            【讨论】:

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

            功劳归Sushil

            像往常一样创建您的 AlertDialog:

            AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
            Dialog dialog = dialog.create();
            dialog.show();
            

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

            dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
            

            【讨论】:

            • 小心。虽然这将设置背景颜色,但您将替换先前包含宽度尺寸的背景可绘制对象。您生成的对话框的大小可能与您未更新背景可绘制对象的大小不同。
            • 我希望我们可以通过主题资源使用一些样式属性来做到这一点。我认为必须有这种可能性来设置不是颜色,而是任何其他可绘制的背景资源。
            【解决方案9】:

            您可以创建自定义 alertDialog 并使用 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);
            

            【讨论】:

            • 但是我可以只添加一行代码来改变颜色而不做所有这些事情吗?
            • 也许你可以试试这个:getWindow().setBackgroundDrawableResource(android.R.color.white);
            • 我希望你把它写在正确的地方,只得到你的 alertDialog 窗口..
            • 如果添加 YOUR_DIALOG.getWindow().setBackgroundDrawableResource(android.R.color.white);它会为你工作......
            【解决方案10】:

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

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

            AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)...
            

            【讨论】:

            • 它说现在已弃用?现在有什么建议吗?
            • @Dinesh 如果你使用的是Android Studio,它会直接告诉你替换:Deprecated Use android.R.style.Theme_Material_Light_Dialog_Alert
            猜你喜欢
            • 1970-01-01
            • 2014-02-24
            • 2013-06-07
            • 2013-12-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多