2013-08-20 62 views
3

我有一個小麻煩讓我的自定義字體顯示出來大膽,這裏就是我想要做的事:安卓顯示HTML代碼,自定義字體爲粗體

我給自己定在一個HTML格式的字符串RES /串

<string name="string1"> 
<![CDATA[<b>Title1</b><p>sub text</p>]]> 
</string> 

然後我設置字體在運行時(其中的getString得到渣油從一個字符串):

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf"); 
TextView body = (TextView) findViewById(R.id.tv_body); 
body.setText(Html.fromHtml(getString(name))); 
body.setTypeface(tf); 

問題是,該文本不顯示爲加粗,它應該,我要去的任何想法錯誤??

很抱歉忘了補充一點,我需要一些文字爲粗體,有些不是。

回答

3

用途:

body.setTypeface(tf, Typeface.BOLD); 

Typeface

您也可以在web視圖設置字體。使用

<html> <head><style type=\"text/css\"> @font-face { font-family: MyFont;  src: url(\"file:///android_asset/font.ttf\") } body { font-family: MyFont; font-size: 12px; text-align: justify; } </style> </head><body> 

和負載:只需添加這樣的事情

webView.loadDataWithBaseURL("file:///android_asset/", result, "text/html", "UTF-8", "null"); 

// FOR EDITED問題

String s = getResources().getString(R.string.string1); 
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE); 
+0

對不起編輯我的問題,我需要一些文字爲粗體一些不。 – Isaac

2

我試着根據你的代碼進行一些修改。

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf"); 
TextView body = (TextView) findViewById(R.id.myTV); 
body.setText(Html.fromHtml(getString(R.string.string1))); 
body.setTypeface(tf); 

它應該工作。而FYR,訪問String資源的正確方法就像R.string.your_string。參考號String Resources