我想在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();
}
他们的示例似乎对格式化字符串有帮助:
将样式化的文本资源存储为HTML转义的字符串:
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
在此格式化的字符串中,添加了一个元素。请注意,使用
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();
请尝试此代码。它工作正常
<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>")
<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>
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);