在AlertDialog中以粗体设置文本

问题描述 投票:2回答:6

我想在AlertDialog中以粗体显示一部分消息文本。

我尝试过:

<b> </b> tag中添加strings.xml,但没有任何积极意义。

我也使用过Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")

我也去过stackoverflow.com,但没有正面结果。

在我的代码下面:

 showDialog(getActivity(),"Sample",Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")+"\n\n"+));



public static void showDialog(Context mContext, String Title,
            String Description) {

        final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

        dialog.setTitle(Title);
//      dialog.setMessage((Html.fromHtml("<b>"+Description+"</b>")));
        dialog.setMessage(Description);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                // TODO Auto-generated method stub

            }
        });

        //

        AlertDialog alert=dialog.create();
//      dialog.show();
        alert.show();


    }
android android-alertdialog
6个回答
3
投票
page描述如何将HTML格式添加到资源字符串。

他们的示例似乎对格式化字符串有帮助:

将样式化的文本资源存储为HTML转义的字符串:

<resources> <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string> </resources>

在此格式化的字符串中,添加了一个元素。请注意,使用

2
投票
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")); builder.setNeutralButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); AlertDialog alert = builder.create(); alert.show();
请尝试此代码。它工作正常

1
投票
您需要在xml中设置粗体文本,例如:

<resource> <string id="@+id/your_message">We are <b><i>so</i></b> glad to see you.</string> </resources>

并在想要加粗文本的特定位置调用它:

Html.fromHtml("<b>"+getString(R.string.your_message)+"</b>")


0
投票
<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>

0
投票
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>

0
投票
我通过在AlertDialog.Builder中创建自己的textView解决了它。

LayoutInflater inflater = LayoutInflater.from(getContext()); View view = inflater.inflate(R.layout.dialog_custom_text_view, null); TextView textView = view.findViewById(R.id.dialog_custom_text_view); textView.setText(message); builder.setView(view);

© www.soinside.com 2019 - 2024. All rights reserved.