Textview fromHtml粗体标记未正确显示

时间:2014-10-27 13:21:44

标签: android html textview

我正在使用HTML来获取某些单词,但它没有正确显示。请说我的英文不好。

        Typeface tfArial = Typeface.createFromAsset(getAssets(), "arialtur.otf");

    String yazi="Deneme "+"<strong>"+"must be bold"+"</strong>"+" kayıt.";
    Spanned text1 = Html.fromHtml(yazi);
    TextView aa= (TextView) findViewById(R.id.metin1);
    TextView ab= (TextView) findViewById(R.id.metin2);

    aa.setText(text1);
    aa.setTypeface(tfArial);        
    ab.setText("Non arial font");

屏幕截图http://hizliresim.com/rd4gkV

4 个答案:

答案 0 :(得分:3)

如果resres字符串中有<b>标记,您可以在<![CDATA[]]>中包含文字,即:

<string name="textWithBold"><![CDATA[<b>BoldText</b>]]></string>

然后你可以得到并显示出来:

textView.setText(Html.fromHtml(getString(R.string.textWithBold)));

答案 1 :(得分:2)

<strong> HTML标记不是“样式”标记。它只是表明内容很重要。 <strong>的默认样式依赖于Web引擎实现。

您可以获得有关这两个链接的一些信息:

如果您想要粗体文字,请尝试使用<b>代替<strong>

答案 2 :(得分:1)

您可以通过以下几种方式执行此操作,例如

Typeface tfArial =Typeface.createFromAsset(getAssets(),"arialtur.otf");
String yazi="Deneme <strong> must be bold </strong> kayıt.";
 // OR  String yazi="Deneme <b> must be bold </b> kayıt.";

TextView aa= (TextView) findViewById(R.id.metin1);
TextView ab= (TextView) findViewById(R.id.metin2);
aa.setText(Html.fromHtml(yazi));
aa.setTypeface(tfArial); 
 //OR  aa.setTypeface(tfArial,Typeface.BOLD);      
ab.setText("Non arial font");

示例:

使用标记

Deneme 必须是粗体kayıt。

使用 b 标记

Deneme 必须是粗体kayıt

答案 3 :(得分:1)

试试这个。

String message ="Deneme "+"<b>"+"must be bold"+"</b>"+" kayıt.";
aa.setText(Html.fromHtml(message));

您也可以使用HTML标记

从XML做同样的事情