【问题标题】:Change color of button in AlertDialog.Builder更改 AlertDialog.Builder 中按钮的颜色
【发布时间】:2016-10-08 08:53:52
【问题描述】:

给定一个函数

private void showInputDialog() {
    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    input.setSingleLine();
    FrameLayout container = new FrameLayout(getActivity());
    FrameLayout.LayoutParams params = new  FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.leftMargin= convertDpToPx(25);                                       //remember to scale correctly
    params.rightMargin= convertDpToPx(30);
    input.setLayoutParams(params);
    container.addView(input);
    alert.setTitle("Change City");
    alert.setMessage("Hey there, could not find the city you wanted. Please enter a new one:\n");
    alert.setView(container);
    alert.setPositiveButton("Go", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            changeCity(input.getText().toString());
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alert.show();
}

现在,当我在我的应用程序中获得此 AlertDialog.Builder 时,按钮颜色为绿色(Android 5 的默认颜色),但 EditText 颜色为粉色(R.color.coloraccent)。如何将按钮的颜色更改为粉红色?

任何帮助将不胜感激

【问题讨论】:

  • 在alerdialog中设置样式

标签: android android-studio-2.2


【解决方案1】:

这样试试

alertDialog.show(); 

仅在调用 .show() 之后。 试试这个sn-p

//for negative side button
    alertDialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor); 
//for positive side button
    alertDialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor);

【讨论】:

    【解决方案2】:

    您可以设置主题。正如您所指出的,这些按钮在您的主题中默认为 textColor 使用强调颜色,要更改它,您应该:

    在 res/values/styles.xml 中修改您的应用主题

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="buttonBarPositiveButtonStyle">@style/Base.Widget.AppCompat.Button.Borderless.MyStyle</item>
    </style>
    

    并将您的按钮样式添加到 AlertDialog 按钮的主题

    <style name="Base.Widget.AppCompat.Button.Borderless.MyStyle">
        <item name="android:textColor">@color/colorAccent</item>
    </style>
    

    注意

    这会影响所有的 AlertDialogs 而不仅仅是一个。您还可以创建一个新的单独主题并添加到 AlertDialog.Builder 中,例如 new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme)

    【讨论】:

    • 这将不起作用,因为 AlertDialog 使用 ?alertDialogThemeAppTheme。您必须覆盖警报对话框主题和其中的按钮样式。
    【解决方案3】:

    您需要在警报窗口中添加自定义视图。创建一个名为“my_custom_alert_window”的布局文件,然后执行以下操作:

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.my_custom_alert_window, null);
    dialogBuilder.setView(dialogView);
    
    Button btn = (Button) dialogView.findViewById(R.id.label_field);
    btn.setBackgroundColor(....);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
    

    然后你可以像上面那样改变按钮的背景颜色

    【讨论】:

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