【问题标题】:How to change color of Button in Alert Dialog [duplicate]如何在警报对话框中更改按钮的颜色[重复]
【发布时间】:2018-03-05 23:31:54
【问题描述】:

这是我创建对话框的代码,

builder.setMessage(msg).setNeutralButton("Dismiss",dialogClickListener)
                       .setPositiveButton("Edit", dialogClickListener)
                       .setNegativeButton("Delete", dialogClickListener).show();

是否可以用蓝色而不是红色显示解雇?

【问题讨论】:

  • Sheikh 下面给出的答案教我关于 AlertDialog dialog = builder.create(); Brahmy adigopula 在原始问题中的答案只是指 AlertDialog.show out of the blue

标签: android message


【解决方案1】:

首先,从builder创建AlertDialog:

AlertDialog dialog = builder.create();

然后你可以找到你的按钮并改变颜色:

dialog.show(); //Only after .show() was called
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(your_color);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(your_color);
dialog.getButton(dialog.BUTTON_NEUTRAL).setTextColor(your_color);

这里你用下一个method:

void setTextColor (int color)
     Sets the text color for all the states (normal, selected, focused) to be this color.
Parameters
     color  int: A color value in the form 0xAARRGGBB. Do not pass a resource ID. To get a color value from a resource ID, call getColor.

【讨论】:

  • 谢谢你,一个有点神秘的答案,但它为我指出了正确的方向,现在它可以工作了。
【解决方案2】:

是的,可以,从样式更改颜色,您可以为对话框定义自定义样式,如下所示:

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">yourColorCode</item>
  <item name="android:colorAccent">yourColorCode</item>
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2016-12-01
    • 2019-11-24
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多