【问题标题】:Cannot change background color of AlertDialog button无法更改 AlertDialog 按钮的背景颜色
【发布时间】:2020-01-01 08:59:59
【问题描述】:

我正在尝试使用 stackoverflow 中为此问题定义的所有解决方案来更改 AlertDialog 的颜色,但它仍然无法正常工作。

这是我用来实现这一目标的代码:

 val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
 val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
 var view = factory.inflate(R.layout.sample, null)
 [...]
 vacationDialog.setView(view)
        val window = vacationDialog.create()
 vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
            dialog.dismiss()

        }

 /**Listener called when the AlertDialog is shown**/


 vacationDialog.show()

 window.setOnShowListener {

 /**Get the positive button from the AlertDialog**/
 val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)

 /**Set your color to the positive button**/
 positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
        }

这是 style.xml 中定义的样式 DialogTheme,顺便说一下,我找不到更改默认按钮颜色(黑色)的方法,并且似乎覆盖了从代码尝试的任何更改。

<style name="DialogTheme" parent="android:Theme.Holo">

        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@color/white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:gravity">center</item>


    </style>

对如何解决这个问题有任何想法吗?

【问题讨论】:

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


    【解决方案1】:

    如果您使用的是 AppCompat 主题,则可以使用以下内容:

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
            <!-- Used for the buttons -->
            <item name="colorAccent">#FFCC00</item>
            <!-- Used for the title and text -->
            <item name="android:textColorPrimary">#FFFFFF</item>
            <!-- Used for the background -->
            <item name="android:background">#5fa3d0</item>
    </style>
    

    有了新的Material components for android library,您可以使用新的androidx.appcompat.app.AlertDialog

    只需使用类似的东西:

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

    您可以使用以下内容自定义主题:

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

    和:

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

    【讨论】:

      【解决方案2】:

      如果您使用custom layout,那么您可以轻松更改任何内容。按照下面的代码:

      private fun showAlertDialog(activity: Activity){
          val dialog = Dialog(activity)
          dialog.setContentView(R.layout.dialog_layout)
          dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
          dialog.show()
      }
      

      【讨论】:

        【解决方案3】:
        • 您可以为 AlertDialog 创建自定义布局,并在扩展 AlertDialog 的自定义类中对其进行扩展。
        • 使用 androidx.appcompat.app.AlertDialog 而不是普通的 android.app.AlertDialog

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-04
          • 1970-01-01
          • 2021-06-22
          • 2015-04-03
          • 1970-01-01
          相关资源
          最近更新 更多