【问题标题】:Changing AlertDialog.Builder button color更改 AlertDialog.Builder 按钮颜色
【发布时间】:2013-07-27 21:17:55
【问题描述】:

我正在尝试更改 AlertDialog.Builder 的按钮颜色,但我没有找到方法。

我想像在 HOLO 主题中一样将按钮和标题的颜色更改为白色。

请参阅以下 2 个屏幕截图以获取示例:

我看过这里:

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>

【问题讨论】:

    标签: android android-dialog


    【解决方案1】:

    我知道这是一个非常古老的问题,但我遇到了同样的问题并找到了解决方案。 要更改警报对话框按钮内的文本颜色,您应该执行以下操作:

    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));
        }
    
    }
    

    您使用的按钮的 xmls :

    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>
    

    我个人在 Fragment 中使用了这段代码,这就是为什么我有一个 LayoutInflater。在您的情况下,您可以跳过此步骤。 希望它在未来对其他人有所帮助。

    【讨论】:

      【解决方案2】:

      为了跟进@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));
      

      但以编程方式执行此操作通常不被认为是最好的方式...

      【讨论】:

        【解决方案3】:

        更改 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));
           }
        

        快乐编码:)

        【讨论】:

          【解决方案4】:

          之后 alertDialog.show();

          Button buttonNegative= alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);  
          buttonNegative.setBackgroundColor(Color.RED);
          

          buttonNegative.setBackgroundColor(getResources().getColor(R.color.red));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-03
            • 2014-07-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多