2011-12-12 62 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

歡迎!請接受它作爲答案:) –