如何在AlertDialog中更改文本颜色 [英] How to change textcolor in AlertDialog

查看:135
本文介绍了如何在AlertDialog中更改文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to change the textcolor in an 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对话框v2

推荐答案

仅更改字体颜色,请尝试以下操作:

For changing the font color only, try this:

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

结果:

要更改字体颜色和按钮背景颜色,请尝试以下操作:

For changing the font color and button background color, try this:

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

结果:

如果要更改分隔线颜色,请尝试以下操作:

If you want to change divider color, try this:

        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"开头.

This is my sample code, but if you want to change the divider color consider the part of the code starts with "int titleDividerId".

结果:

如果您想大量自定义AlertDialog.例如,添加一些具有自定义背景色的复选框,请使用以下方法:

If you want to customize the AlertDialog a lot. For example adding some checkboxes with custom background color, use this approach:

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

结果:

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

Firstly, I created a main layout (vertical) as you see in the code. Then, for each one of the checkboxes I created a horizontal layout. In this case you can play with the colors and fonts of the elements (checkboxes, items, and etc.). I hope it helps.

这篇关于如何在AlertDialog中更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆