2016-10-02 221 views
2

enter image description hereAlertDialog变化肯定按钮颜色

我想在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" /> 

+0

是不是应该回答你的问题 - http://stackoverflow.com/a/31514677/632516? – Divers

+0

@Divers不,我不能做这样的,'colorAccent'将如果您需要更改只有一个按钮改变消极和积极的颜色 –

+0

的,你应该通过Java这样做,因为这是一个哈克的解决方案,而不是下面的设计原则的android。如何通过java来完成 - 请检查我上面的链接。 – Divers

回答

1

有没有办法通过样式来做到这一点,因为一般是从的观点UI点错了。如果你想这样做,只需通过java。

+0

如何改变styles/xml对话框按钮的颜色?您应该避免在Java代码中进行静态UI更改,不惜一切代价。 –

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

希望它能帮助!干杯!

+0

“我不想在java代码中改变按钮的颜色。” – Divers

+0

@Divers,如果你知道,为什么你不发表答案? :/ –

+0

无论如何谢谢@ibtehaz,我知道如何通过java更改单个按钮的文本颜色。你发布的方法是真正正确的,但很抱歉,这不是我想要的。 –

3

注意:不建议解决方案,因为这实际上是一个哈克的解决方案。这不是遵循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