AlertDialog更改正面按钮颜色

6

enter image description here

我希望在AlertDialog中更改正按钮的颜色,下面是我的操作:

// style.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:buttonBarPositiveButtonStyle">@style/positive</item>
</style>

<style name="positive">
    <item name="android:textColor">@color/accent</item>
</style>

我使用以下方式来应用样式:

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

但我所做的并没有起作用,我只想改变积极按钮的颜色。我不想在Java代码中更改按钮的颜色。

提前感谢:-)

编辑-1 AlertDialog布局XML

<android.support.v7.internal.widget.ButtonBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="bottom"
app:allowStacking="@bool/abc_allow_stacked_button_bar"
style="?attr/buttonBarStyle">

<Button
    android:id="@android:id/button3"
    style="?attr/buttonBarNeutralButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.v4.widget.Space
    android:id="@+id/spacer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:visibility="invisible" />

<Button
    android:id="@android:id/button2"
    style="?attr/buttonBarNegativeButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@android:id/button1"
    style="?attr/buttonBarPositiveButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


如果您只需要更改一个按钮,您应该通过Java来进行操作,因为这是一种hacky解决方案,并且不遵循Android的设计原则。如何通过Java进行操作 - 请查看我上面的链接。 - Divers
我知道通过Java如何改变颜色。是的,这是一个俗气的解决方案。但是,即使我最终不使用这个解决方案,我仍然很好奇为什么我无法成功地改变它。 - Dennis Lu
@zilk 实际上,看起来可以通过样式进行更改。破坏性对话框 - Dennis Lu
@zilk 你可以看到我的Edit-1,材料对话框的源代码,你可以在appcompatv7中找到它,积极按钮使用一个名为buttonBarPositiveButtonStyle的属性。 - Dennis Lu
如何更改几乎任何视图的背景图片?通过使用样式或者(间接地)通过使用StateList Selector。简单。 - Phantômaxx
显示剩余6条评论
3个回答

10

注意:这种解决方案并不推荐使用,因为这实际上是一种hacky的解决方案。它没有遵循Android的设计原则,不应该被使用。我通过Java改变了正按钮的颜色。感谢@Divers指出这一点。

styles.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarPositiveButtonStyle">@style/positive</item>
</style>

<style name="positive">
    <item name="android:textColor">@color/accent</item>
</style>

使用方法

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

也许您需要注意的一点是,应注意使用 buttonBarPositiveButtonStyle 而不是 android:buttonBarPositiveButtonStyle


5
builder.setOnShowListener( new OnShowListener() {
  @Override
  public void onShow(DialogInterface arg0) {
       builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(COLOR_YOU_WANT);
  }
}

希望这能有所帮助!祝好!

我不想在Java代码中更改按钮的颜色。 - Divers
@Divers,如果你知道,为什么不发表答案? :/ - O_o
无论如何感谢@ibtehaz,我知道如何通过Java更改单个按钮的文本颜色。您发布的方法确实是正确的,但抱歉,这不是我想要的。 - Dennis Lu
@naXa,我猜你已经解决了这个问题。我在度假期间。但是如果你还卡住了,请开一个新的问题。因为“无法解析方法setOnShowListener”可能意味着很多事情,从你的gradle需要重新同步,你需要重新启动你的项目... - O_o
1
这里不应该说builder - 这很容易让人困惑。OnShowListener()是AlertDialog的一个方法,而不是builder的方法。因此,您需要使用builder.create()创建对话框,然后才能找到该方法。@O_o,您没有提供有用的评论。 - d4c0d312
@d4c0d312,我已经三年没有专业进行Android开发了。当我写下那个答案时,我相当确定自己知道在做什么。但是,现在,我不知道你在说什么。也许,我们不应该从一个三年前的答案中获取提示,除非它是如何关闭VIM的。@_@ - O_o

0

无法通过样式来实现,因为从 UI 角度来看通常是错误的。如果您仍想这样做,可以通过 Java 实现。


@RyanNewsom 你没理解我的意思。我的意思是这样做是错误的,因为你打破了系统的标准行为。虽然我知道谷歌经常会忘记提供API。 - Divers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接