更改对话框按钮颜色

问题描述 投票:0回答:3

有什么方法可以更改默认对话框按钮的颜色,或者我需要为此做自定义对话框吗?

这是我的对话:

private void removeItem(final int position) {
    /** Create dialog which ask if user is sure about delete name from list **/
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Delete player");
    builder.setIcon(R.mipmap.ic_launcher);
    builder.setMessage("Do you want to delete player: \"" + mNameList.get(position).getText1() + "\"?")
            .setCancelable(false)

            /** If user click "ok" button, delete player from list and save changes **/
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mNameList.remove(position);
                    mAdapter.notifyItemRemoved(position);
                    saveData();
                }
            })

            /** If user click "cancel" button, name won't delete from list **/
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

    /** Create Dialog **/
    AlertDialog alert = builder.create();
    alert.show();
}
android material-design android-alertdialog material-components-android material-components
3个回答
31
投票

您可以使用材料组件库提供的

MaterialAlertDialogBuilder允许创建材料
AlertDialog
.

只需使用:

   new MaterialAlertDialogBuilder(MainActivity.this,
       R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setMessage("Lorem ipsum dolor ....")
              .setPositiveButton("Ok", /* listener = */ null)
              .setNegativeButton("Cancel", /* listener = */ null)
              .show();

然后定义您的自定义样式,使用

buttonBarPositiveButtonStyle
buttonBarNegativeButtonStyle
属性:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
     <item name="buttonBarNeutralButtonStyle">....</item>
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="backgroundTint">@color/secondaryColor</item>
  </style>


0
投票

如果您使用的是Material 3,则有新的项目和主题来设计您的对话框:

实施对话主题

将主题属性 materialAlertDialogTheme 设置为您的自定义 ThemeOverlay 将影响所有对话框。

res/values/themes.xml

<style name="Theme.App" parent="Theme.Material3.*">
  ...
  <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>
</style>

<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
    <item name="colorPrimary">@color/shrine_pink_100</item>
    <item name="colorSecondary">@color/shrine_pink_100</item>
    <item name="colorSurface">@color/shrine_pink_light</item>
    <item name="colorOnSurface">@color/shrine_pink_900</item>
    <item name="alertDialogStyle">@style/MaterialAlertDialog.App</item>
    <item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.App.Title.Text</item>
    <item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button</item>
    <item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button</item>
</style>

res/values/styles.xml

<style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.Material3">
    <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
    <item name="shapeAppearanceOverlay">@null</item>
</style>

<style name="MaterialAlertDialog.App.Title.Text" parent="MaterialAlertDialog.Material3.Title.Text">
     <item name="android:textColor">@color/shrine_pink_900</item>
</style>

  <style name="Widget.App.Button" parent="Widget.Material3.Button.TextButton.Dialog">
    <item name="android:textColor">@color/shrine_pink_900</item>
  </style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.Material3.MediumComponent">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">8dp</item>
</style>

或者,如果您只想更改一个特定的对话框,请将

themeResId
传递给构造函数:

MaterialAlertDialogBuilder(context,  R.style.ThemeOverlay_App_MaterialAlertDialog)
    ...
    .show()

参考:https://github.com/material-components/material-components-android/blob/master/docs/components/Dialog.md#theming-dialogs


-1
投票

你可以像这样给你的警报

style
dialog

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);

你可以像这样在

xml
文件中写这个样式:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:colorAccent">#f3f3f3</item>
    <item name="android:textColor">#f3f3f3</item>
    <item name="android:textColorPrimary">#f3f3f3</item>
</style>
© www.soinside.com 2019 - 2024. All rights reserved.