2016-10-02 217 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