android:字符串格式指定粗体

android:字符串格式指定粗体,android,Android,我在string.xmllike中定义了一个字符串 <string name="eventtitle">Title: %1$s </string> 标题:%1$s 使用string.format对其进行格式化。如何定义字符串以仅将Titel:设置为粗体。 提前感谢您的帮助您必须在显示的文本视图中设置字符串的样式。查看此您可以使用HTML标记,如“粗体其他文本…” 有关更多信息,请参阅。您可以像 textView.setText(Html.fromHtml("<b

我在
string.xml
like中定义了一个字符串

<string name="eventtitle">Title: %1$s </string>
标题:%1$s
使用
string.format
对其进行格式化。如何定义字符串以仅将Titel:设置为粗体。


提前感谢您的帮助

您必须在显示的文本视图中设置字符串的样式。查看此

您可以使用HTML标记,如
“粗体其他文本…”

有关更多信息,请参阅。

您可以像

textView.setText(Html.fromHtml("<b>Title</b>: Text"));
textView.setText(Html.fromHtml(“Title:Text”);
如果您有动态方式的文本

要在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>
此文本使用粗体和斜体
通过使用内联标记,例如在字符串文件中。
请参见

int upto=strt\u title\u desc.indexOf(“:”)//您可以指定5个

 if (strt_title_desc!=null)
    {
        aboutAuthTV.setTextColor(Color.BLACK);
        aboutAuthTV.setLineSpacing(1.2f, 1.5f);
        aboutAuthTV.setTextSize(23);
    SpannableString SS = new SpannableString(strt_title_desc);
    SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    yourtextView.setText(SS);
}
//此选项用于更改字体大小、样式和颜色

String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
                         <i>Then Italic</i> </font>" +
                       "<br/> <font color=\"green\" >this is simple sentence </font>" +
                       "<br/><br/><br/><br/><a>this is simple sentence</a>";
      Spanned strHtml= Html.fromHtml(str);

       TextView tv = (TextView)findViewById(R.id.textView);
       tv.setText(strHtml);
String str=“Bold
然后是普通文本
然后是斜体“+ “
这是一个简单的句子”+ “




这是一个简单的句子”; span strHtml=Html.fromHtml(str); TextView tv=(TextView)findViewById(R.id.TextView); tv.setText(strHtml);
现在,它得到了合理的支持

您可以将xml中的字符串定义为
Hey b>这是粗体的/b>

然后在代码中,使用此命令将其转换为CharSequence,然后将其用于例如TextView中

String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);

现在,这是合理的支持


我已经给出了这个问题的答案

事实上,很多答案已经过时了。经过自己的研究,我发现最好的答案是@Wahib。以下是改进的版本:

将字符串资源定义为:

<string name="styled_text">Hey, &lt;b>this is bold&lt;/b> text</string>
结果如下:


我知道这有点旧,但@bvd不正确。字符串可以通过
HTML在string.xml中按照“您可以使用HTML标记为字符串添加样式…”设置样式。fromHtml()现在不推荐使用了。
您在哪里发现它不推荐使用了?他们在API 24中添加了新方法。您两次回答了相同的问题-查找:
<string name="styled_text">Hey, &lt;b>this is bold&lt;/b> text</string>
String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);