2013-07-27 271 views
5

我試圖改變AlertDialog.Builder的按鈕顏色,但我沒有找到辦法做到這一點。更改AlertDialog.Builder按鈕顏色

我想要改變按鈕的顏色和標題爲白色,就像在HOLO主題。

看到這些2個截圖例子:

enter image description here

enter image description here

伊夫看着這裏:

How to change theme for AlertDialog

Change the style of AlertDialog

How to change the background of the custom alert dialog

Applying Styles Android

所有這些都沒有爲我工作。

這裏是我的代碼:

public void logInDialog() 
{ 
    ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.dialogStyle); 
    AlertDialog.Builder builder = new AlertDialog.Builder(ctw); 
    builder.setTitle("Log in"); 
    View prefView = View.inflate(this, R.layout.log_in, null); 
    //The rest of the code......... 
} 

這是我的風格代碼:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="dialogStyle" parent="android:Theme.Dialog"> 
     <item name="android:background">@color/white</item> 
     <item name="android:layout_width">wrap_content</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:button">@color/white</item> 
    </style>  
</resources> 

回答

17

我知道這是一個非常古老的問題,但我同樣的問題遇到了,我發現一個解。 爲了改變文本的顏色的警報對話框的按鈕內,你應該做這樣的事情:

public void logInDialog() 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    LayoutInflater inflater = context.getLayoutInflater(); 

    //setting custom view for our dialog 
    View myview = inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT, null); 
    builder.setNeutralButton(android.R.string.cancel, null); 
    builder.setView(myview); 

    //creating an alert dialog from our builder. 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 

    //retrieving the button view in order to handle it. 
    Button neutral_button = dialog.getButton(DialogInterface.BUTTON_NEUTRAL); 

    Button positive_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE); 


    if (neutral_button != null) { 
     neutral_button.setBackgroundDrawable(context.getResources() 
         .getDrawable(R.drawable.custom_background)); 

     neutral_button.setTextColor(context.getResources() 
         .getColor(android.R.color.white)); 
    } 
    if (positive_button != null) { 
     positive_button.setBackgroundDrawable(context.getResources() 
         .getDrawable(R.drawable.custom_background)); 

     positive_button.setTextColor(context.getResources() 
         .getColor(android.R.color.white)); 
    } 

} 

而且按鈕所處的個XML使用:

custom_background.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item android:drawable="#000000"/> 
    <item android:drawable="@drawable/selectable_item_background"/> 

</layer-list> 

而且selectable_item_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/item_pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/item_focused" android:state_focused="true"/> 
    <item android:drawable="@drawable/item_focused" android:state_selected="true"/> 
    <item android:drawable="@android:color/transparent"/> 

</selector> 

我個人在片段內使用了這段代碼,這就是爲什麼我有一個LayoutInflater。在你的情況下,你可以跳過這一步。 希望它能幫助未來的其他人。

+1

大,由於一噸! – Malfunction

0

更改AlertDialog的按鈕顏色。

// Initialize AlertDialog & AlertDialog Builder 
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); 
builder.setTitle(R.String.AlertDialogTitle); 
........... 
......... 
//Build your AlertDialog 
AlertDialog Demo_alertDialog= builder.create(); 
Demo_alertDialog.show(); 

//For Positive Button: 
Button b_pos; 
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); 
if(b_pos!=null){ 
    b_pos.setTextColor(getResources().getColor(R.color.YourColor)); 
    }  


//For Neutral Button: 
Button b_neu; 
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL); 
if(b_neu!=null){ 
    b_neu.setTextColor(getResources().getColor(R.color.YourColor)); 
    } 

//For Negative Button: 
Button b_neg; 
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE); 
if(b_neg!=null){ 
    b_neg.setTextColor(getResources().getColor(R.color.YourColor)); 
    } 

編碼快樂:)

0

爲了跟進@ Ioumaros的回答,setBackgroundDrawable現在已經過時。您可以使用此代碼實現相同的背景顏色變化:

Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE); 
    negativeButton.setBackgroundColor(getResources().getColor(R.color.colorBackground)); 

    Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); 
    positiveButton.setBackgroundColor(getResources().getColor(R.color.colorBackground)); 

但是編程這樣做是不是通常認爲的最佳方式......