【问题标题】:How to customize Alert Dialog buttons like in Google Photos App如何自定义警报对话框按钮,如 Google Photos App 中的
【发布时间】:2019-05-29 00:15:54
【问题描述】:

如何实现这样的警报对话框按钮样式:

我是否必须创建自定义“对话框”或者这可以通过 AlertDialog 主题来实现?

【问题讨论】:

标签: android material-design android-alertdialog material-components-android material-components


【解决方案1】:

只需使用自定义样式的MaterialAlertDialogBuilder

  new MaterialAlertDialogBuilder(MainActivity.this, 
            R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Keep backup off?")
              .setMessage("Backup is free and unlimited at high quality")
              .setPositiveButton("Turn on", null)
              .setNegativeButton("Keep off", /* listener = */ null)
              .show();

使用buttonBarPositiveButtonStylebuttonBarNegativeButtonStyle 属性更改默认颜色。
在这种情况下,您可以将filled button (Widget.MaterialComponents.Button) 用于肯定按钮,将Text Button (Widget.MaterialComponents.Button.TextButton.Dialog) 用于否定按钮。

  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">

    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
  </style>

  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">@color/primaryDarkColor</item>
  </style>

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

【讨论】:

    【解决方案2】:

    你应该使用这个库来进行漂亮的对话

    https://github.com/pedant/sweet-alert-dialog

    【讨论】:

      【解决方案3】:

      您可以像创建普通活动屏幕一样自定义对话屏幕,但您搜索不同的对话屏幕尝试 this 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 2011-08-08
        • 2014-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多