在对话框消息中显示带有粗体字母的字符串
Display string with bold letters in Dialog message
假设我有这个:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setMessage("I want THIS word to be bold and the rest normal!")
.create().show();
有什么方法可以 Android 对部分文本字符串应用格式或粗体?
您必须使用自定义对话框。默认 Android 对话框没有执行此操作的任何选项。
有关详细信息,请参阅:
Custom Dialog Android
您可以像这样向 strings.xml 添加一个字符串:
<string name="bold_message">I want <b>THIS</b> word to be bold and the rest normal!</string>
然后在您的警报对话框中使用 setMessage(R.string.bold_message)
使用 Spannable 字符串很简单
在您的 string.xml 中添加
<string name = "bold_message"><![CDATA[<p>This want <b>THIS</b> word to bold and the rest normal!</p>]]></string>
在您的提醒对话框中
.setMessage(Html.fromHtml(R.string.bold_message);
一些额外的信息。
假设 string resource
就像
<string name="bold_text">This is <b>bold</b> text.</string>
将此 string resource
设置为 Dialog
,如 .setMessage(R.string.bold_text)
或 CharSequence
,如 .setMessage(context.getResource.getText(R.string.bold_text))
。
.setMessage(context.getString(R.string.bold_text))
将正常显示整个文本(无粗体)。
假设我有这个:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setMessage("I want THIS word to be bold and the rest normal!")
.create().show();
有什么方法可以 Android 对部分文本字符串应用格式或粗体?
您必须使用自定义对话框。默认 Android 对话框没有执行此操作的任何选项。
有关详细信息,请参阅:
Custom Dialog Android
您可以像这样向 strings.xml 添加一个字符串:
<string name="bold_message">I want <b>THIS</b> word to be bold and the rest normal!</string>
然后在您的警报对话框中使用 setMessage(R.string.bold_message)
使用 Spannable 字符串很简单
在您的 string.xml 中添加
<string name = "bold_message"><![CDATA[<p>This want <b>THIS</b> word to bold and the rest normal!</p>]]></string>
在您的提醒对话框中
.setMessage(Html.fromHtml(R.string.bold_message);
一些额外的信息。
假设 string resource
就像
<string name="bold_text">This is <b>bold</b> text.</string>
将此 string resource
设置为 Dialog
,如 .setMessage(R.string.bold_text)
或 CharSequence
,如 .setMessage(context.getResource.getText(R.string.bold_text))
。
.setMessage(context.getString(R.string.bold_text))
将正常显示整个文本(无粗体)。