2011-12-12 65 views
4

我在string.xml定义如下安卓:字符串格式指定粗体

<string name="eventtitle">Title: %1$s </string> 
这是使用 string.format格式化

的字符串。如何定义字符串以获得只有标题:如粗体。

在此先感谢您的帮助

回答

2

你有你的风格在这显示TextView的字符串。看到这个link

+2

我知道这是有点老了,但是这是不正确@bvd。字符串可以根据 在string.xml中设置样式“您可以使用HTML标记将样式添加到字符串中......” via http://developer.android.com/guide/topics/resources/string-resource.html – MikeIsrael

21

你可以不喜欢它,

textView.setText(Html.fromHtml("<b>Title</b>: Text")); 

如果您有动态的方式文本..

,并定义formatings在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> 

This Link

+1

'Html.fromHtml()'现在已被弃用 –

+1

你从哪里发现它已被弃用?他们在API 24中添加了新方法。 – HopefullyHelpful

0
Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf"); 
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF"); 

String strt_title_desc=this.getResources().getString(R.string.eventtitle); 

int upto=strt_title_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); 
6

现在,人们通过合理支持Android

可以定义XML字符串作为<string name="text_to_show">Hey &lt;b>This is in bold&lt;/b>

然后在代码中,使用此转换为CharSequence的,然后在TextView中使用它如现在

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

第一个解决方案有效。谢谢 – akash89

+0

欢迎!请接受它作为答案:) –