Display string with bold letters in Dialog message

岁酱吖の 提交于 2019-12-25 04:49:10

问题


Let's say I had this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
    .setMessage("I want THIS word to be bold and the rest normal!")
    .create().show();

Is there any way to have Android apply formatting or bold font to parts of the text string?


回答1:


You can add a string to your strings.xml like this:

<string name="bold_message">I want <b>THIS</b> word to be bold and the rest normal!</string>

and then in your alert dialog just use setMessage(R.string.bold_message)




回答2:


Using Spannable string is easy way

In your string.xml add

<string name = "bold_message"><![CDATA[<p>This want <b>THIS</b> word to bold and the rest normal!</p>]]></string>

In your alert dialog

.setMessage(Html.fromHtml(R.string.bold_message);



回答3:


You must use custom dialogs . Default Android dialogs don't have any option to do this .
For more info refer to : Custom Dialog Android



来源:https://stackoverflow.com/questions/40430115/display-string-with-bold-letters-in-dialog-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!