警报对话框按钮具有不同的颜色设置 [英] Alert dialog button has different colorset

查看:68
本文介绍了警报对话框按钮具有不同的颜色设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个警报对话框.按钮是紫色的.是的,我可以使用以下方法更改文本的颜色:

  alertDialog.getButton(Dialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor(#000000")); 

无论何时触摸它们,都会形成紫色调.我希望色相像我的应用程序的主要强调一样是蓝色,而文本则是黑色.

这是我的提醒样式:

 < style name ="MyCustomAlert2" parent ="Theme.MaterialComponents.Dialog.Alert">< item name ="colorAccent"> @ color/materialBlue</item>< item name ="android:textColorPrimary"> @android:color/black</item>< item name ="android:windowBackground"> @ drawable/custom_alert2</item>< item name ="colorControlNormal"> @android:color/black</item>< item name ="colorControlActivated"> @ color/materialBlue</item>< item name ="textColorAlertDialogListItem"> @android:color/black</item></style> 

注意:上面链接的颜色都不是紫色的.

感谢您的帮助

解决方案

使用新的

您可以使用以下方法在应用程序主题中全局设置样式:

 < item name ="materialAlertDialogTheme"> @ style/MyThemeOverlay.MaterialComponents.MaterialAlertDialog</item> 

或者您可以使用构造函数:

 新的MaterialAlertDialogBu​​ilder(上下文,R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog) 

hello, I have an alert dialog. the buttons are purple. yes I can change the color of the text inside using:

          alertDialog.getButton(Dialog.BUTTON_NEGATIVE).
                    setTextColor(Color.parseColor("#000000"));

however when they are touched, there is a purple hue that forms. I want the hue to be blue like the main accent of my app and the text to be black.

this is my alert style:

<style name="MyCustomAlert2" parent="Theme.MaterialComponents.Dialog.Alert">
    <item name="colorAccent">@color/materialBlue</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:windowBackground">@drawable/custom_alert2</item>
    <item name="colorControlNormal">@android:color/black</item>
    <item name="colorControlActivated">@color/materialBlue</item>
    <item name="textColorAlertDialogListItem">@android:color/black</item>
</style>

note: none of the colors linked above are purple.

thanks for the help

解决方案

Use new theandroidx.appcompat.app.AlertDialog and the Material components for android library.

Just use something like:

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

Then you can customize the style with something like:

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

and here you can change each button:

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

You can set the style globally in your app theme using:

<item name="materialAlertDialogTheme">@style/MyThemeOverlay.MaterialComponents.MaterialAlertDialog</item>

or you can use the constructor:

new MaterialAlertDialogBuilder(context,
          R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)

这篇关于警报对话框按钮具有不同的颜色设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆