gpt4 book ai didi

Android 使用自定义字体将 HTML 文本显示为粗体

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:51 24 4
gpt4 key购买 nike

我在让我的自定义字体显示为粗体时遇到了一些麻烦,这是我正在尝试做的事情:

我在 res/string 中设置了一个 HTML 格式的字符串

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

然后我在运行时设置字体(其中 getString 从字符串中获取 resID):

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);

问题是文本在应该显示的地方没有显示为粗体,我哪里出错了??

抱歉忘了补充一点,我需要一些粗体文本,一些不需要。

最佳答案

使用:

body.setTypeface(tf, Typeface.BOLD);

Typeface

您还可以在 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>

并加载使用:

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);

关于Android 使用自定义字体将 HTML 文本显示为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com