Android使用自定义字体将HTML文本显示为粗体
让我的自定义字体显示为粗体有点困难,下面是我尝试做的: 我在res/string中设置了一个HTML格式的字符串Android使用自定义字体将HTML文本显示为粗体,android,android-fonts,Android,Android Fonts,让我的自定义字体显示为粗体有点困难,下面是我尝试做的: 我在res/string中设置了一个HTML格式的字符串 <string name="string1"> <![CDATA[<b>Title1</b><p>sub text</p>]]> </string> 问题是,文本在应该显示的地方没有显示为粗体,有什么想法我错了吗 抱歉,忘了添加我需要一些粗体文本和一些非粗体文本。我已尝试根据您的代码进行一些更改
<string name="string1">
<![CDATA[<b>Title1</b><p>sub text</p>]]>
</string>
问题是,文本在应该显示的地方没有显示为粗体,有什么想法我错了吗
抱歉,忘了添加我需要一些粗体文本和一些非粗体文本。我已尝试根据您的代码进行一些更改
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
。参考号 使用:
body.setTypeface(tf, Typeface.BOLD);
您还可以在webView上设置字体。只需添加如下内容:
<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>
//用于编辑问题
String s = getResources().getString(R.string.string1);
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);
对不起,编辑了我的问题,我需要一些粗体的文本,有些不是。
webView.loadDataWithBaseURL("file:///android_asset/", result, "text/html", "UTF-8", "null");
String s = getResources().getString(R.string.string1);
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);