我在string.xml
定義如下安卓:字符串格式指定粗體
<string name="eventtitle">Title: %1$s </string>
這是使用
string.format
格式化
的字符串。如何定義字符串以獲得只有標題:如粗體。
在此先感謝您的幫助
我在string.xml
定義如下安卓:字符串格式指定粗體
<string name="eventtitle">Title: %1$s </string>
這是使用
string.format
格式化
的字符串。如何定義字符串以獲得只有標題:如粗體。
在此先感謝您的幫助
你有你的風格在這顯示TextView的字符串。看到這個link
您可以使用像"<b>BOLD</b> other text ..."
這樣的HTML標記。
有關更多信息,請參閱this Google resource。
你可以不喜歡它,
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>
'Html.fromHtml()'現在已被棄用 –
你從哪裏發現它已被棄用?他們在API 24中添加了新方法。 – HopefullyHelpful
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);
現在,人們通過合理支持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);
第一個解決方案有效。謝謝 – akash89
歡迎!請接受它作爲答案:) –
我知道這是有點老了,但是這是不正確@bvd。字符串可以根據 在string.xml中設置樣式「您可以使用HTML標記將樣式添加到字符串中......」 via http://developer.android.com/guide/topics/resources/string-resource.html – MikeIsrael