我有一个字符串定义string.xml类似
<string name="eventtitle">Title: %1$s </string>
Run Code Online (Sandbox Code Playgroud)
使用格式化string.format.如何定义字符串只获得Titel:如Bold.
在此先感谢您的帮助
MKJ*_*ekh 23
你可以这样做,
textView.setText(Html.fromHtml("<b>Title</b>: Text"));
Run Code Online (Sandbox Code Playgroud)
如果你有动态的文字..
要在Strings.xml中定义格式化,你可以这样做,
<string name="text1">This text uses <b>bold</b> and <i>italics</i>
by using inline tags such as <b> within the string file.</string>
Run Code Online (Sandbox Code Playgroud)
请参阅此链接
现在,它得到了Android的合理支持.
你可以在xml中定义字符串 <string name="text_to_show">Hey <b>This is in bold</b>
然后在代码中,使用它转换为CharSequence,然后将其用于例如TextView
String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);
Run Code Online (Sandbox Code Playgroud)
小智 8
事实上,很多答案已经过时了。经过我自己的研究,我发现最好的答案是@Wahib 的答案。这是改进的版本:
将字符串资源定义为:
<string name="styled_text">Hey, <b>this is bold</b> text</string>
Run Code Online (Sandbox Code Playgroud)
像这样使用资源:
String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);
Run Code Online (Sandbox Code Playgroud)
结果如下:
| 归档时间: |
|
| 查看次数: |
25553 次 |
| 最近记录: |