【问题标题】:How to change textcolor in AlertDialog如何在 AlertDialog 中更改文本颜色
【发布时间】:2015-10-30 13:39:48
【问题描述】:

如何更改 AlertDialog 中的文本颜色?

<item name="android:textColor">@color/black_text</item>

这只会改变标题颜色。

ad = new AlertDialog.Builder((new ContextThemeWrapper(context, R.style.DialogTheme)));
            ad.setTitle(R.string.my_activ_remove_title_dialog);

            ad.setPositiveButton(R.string.my_activ_remove_dialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {
                    content.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, content.size());
                }

            });
            ad.setNegativeButton(R.string.my_activ_cancel_remove_dialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {

                }
            });

Alerd Dialog v2

【问题讨论】:

    标签: android


    【解决方案1】:

    如果只改变字体颜色,试试这个:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
        builder.setPositiveButton(Html.fromHtml("<font color='#FF7F27'>Yes</font>"), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                Log.e(LOG_TAG, "Yes");
            }
        });
        builder.setNegativeButton(Html.fromHtml("<font color='#FF7F27'>No</font>"), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                Log.e(LOG_TAG, "No");
            }
        });
        builder.create();
        builder.show();
    

    结果:


    要改变字体颜色和按钮背景颜色,试试这个:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
        builder.setCancelable(false);
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
        Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        //Set negative button background
        nbutton.setBackgroundColor(Color.MAGENTA);
        //Set negative button text color
        nbutton.setTextColor(Color.YELLOW);
        Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        //Set positive button background
        pbutton.setBackgroundColor(Color.YELLOW);
        //Set positive button text color
        pbutton.setTextColor(Color.MAGENTA);
    

    结果:


    如果你想改变分隔线的颜色,试试这个:

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Test Title");
            builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
            builder.setCancelable(false);
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            try {
                Resources resources = dialog.getContext().getResources();
                int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
                TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
                alertTitle.setTextColor(Color.MAGENTA); // change title text color
    
                int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
                View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
                titleDivider.setBackgroundColor(Color.YELLOW); // change divider color
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
            //Set negative button background
            nbutton.setBackgroundColor(Color.MAGENTA);
            //Set negative button text color
            nbutton.setTextColor(Color.YELLOW);
            Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            //Set positive button background
            pbutton.setBackgroundColor(Color.YELLOW);
            //Set positive button text color
            pbutton.setTextColor(Color.MAGENTA);
    

    这是我的示例代码,但如果您想更改分隔线颜色,请考虑代码部分以“int titleDividerId”开头。

    结果:


    如果你想自定义 AlertDialog 很多。例如添加一些带有自定义背景颜色的复选框,使用这种方法:

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
            LinearLayout mainLayout       = new LinearLayout(this);
            mainLayout.setOrientation(LinearLayout.VERTICAL);
    
            LinearLayout layout1       = new LinearLayout(this);
            layout1.setOrientation(LinearLayout.HORIZONTAL);
            CheckBox cb1 = new CheckBox(getApplicationContext());
            cb1.setText("Easy");
            layout1.addView(cb1);
            layout1.setBackgroundColor(Color.BLUE);
            layout1.setMinimumHeight(50);
    
            LinearLayout layout2       = new LinearLayout(this);
            layout2.setOrientation(LinearLayout.HORIZONTAL);
            layout2.addView(new TextView(this));
            CheckBox cb2 = new CheckBox(getApplicationContext());
            cb2.setText("Normal");
            layout2.addView(cb2);
            layout2.setBackgroundColor(Color.CYAN);
            layout2.setMinimumHeight(50);
    
            LinearLayout layout3       = new LinearLayout(this);
            layout3.setOrientation(LinearLayout.HORIZONTAL);
            CheckBox cb3 = new CheckBox(getApplicationContext());
            cb3.setText("Hard");
            layout3.addView(cb3);
            layout3.setBackgroundColor(Color.GREEN);
            layout3.setMinimumHeight(50);
    
            mainLayout.addView(layout1);
            mainLayout.addView(layout2);
            mainLayout.addView(layout3);
            alert.setTitle("Custom alert demo");
            alert.setView(mainLayout);
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "done", Toast.LENGTH_SHORT).show();
                }
            });
    
            alert.show();
    

    结果:

    首先,我创建了一个主布局(垂直),正如您在代码中看到的那样。然后,我为每个复选框创建了一个水平布局。在这种情况下,您可以使用元素(复选框、项目等)的颜色和字体。我希望它有所帮助。

    【讨论】:

    • AlertDialog alert = builder.create(); - 建设者无法解决
    • 第一:AlertDialog.Builder builder = new AlertDialog.Builder(this);然后:AlertDialog alert = builder.create();
    • 你有没有:"import android.app.AlertDialog;"在你的进口中?
    • 导入android.support.v7.app.AlertDialog;
    • 将其更改为“import android.app.AlertDialog;”。我的意思是删除该行并添加该行。
    【解决方案2】:

    创建对话框后:

    AlertDialog dialog = builder.create();
    dialog.show();
    Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
    Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));
    

    【讨论】:

    • 它对我有用。但我不知道为什么 "builder.setPositiveButton(Html.fromHtml("Yes"), ..." 对我不起作用
    • 它对我有用.. 但请记住你必须添加 dialog.show();而不是 builder.show();那么只有它会起作用。
    【解决方案3】:

    文本颜色取自styles.xml中设置的colorAccent值(根据您在活动中设置的样式)。

    【讨论】:

      【解决方案4】:

      您可以按照以下几个步骤操作:

      1.添加一个样式并命名为任何你想要的,在我的例子中是AlertDialogTheme

      <style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
          <item name="colorAccent">@color/colorPrimaryDark</item>
      </style>
      

      2.将您的样式作为AlertDialog.Builder() 实例的第二个参数传递。请注意,如果您从 Activity 创建实例,请将第一个参数替换为 this

      AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity(), R.style.AlertDialogTheme);
      
      1. 查看您的更改

      【讨论】:

      • @color/teal_200@color/teal_200 ...适合我。谢谢。
      【解决方案5】:

      对于“材料设计”警告对话框:

      <style name="MyTheme" parent="MaterialAlertDialog.MaterialComponents">
          <item name="materialAlertDialogTitleTextStyle">@style/AlertDialogTitleText</item>
          <item name="materialAlertDialogBodyTextStyle">@style/AlertDialogBodyText</item>
      </style>
      
      <style name="AlertDialogTitleText" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
          <item name="android:textColor">@android:color/black</item>
      </style>
      
      <style name="AlertDialogBodyText" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
          <item name="android:textColor">@android:color/white</item>
      </style>
      

      对话框应该使用 Material builder 构建,并且需要设置样式。

      new MaterialAlertDialogBuilder(getContext(), R.style.MyTheme)
                  .setTitle("Title")
                  .setMessage("Message");
                  
      

      【讨论】:

        猜你喜欢
        • 2019-04-11
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多