简体   繁体   中英

Textview fromHtml bold tag not properly shown

I am using fromHtml for some words, but it not properly shown.Sorry my bad english.

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

screenshot http://hizliresim.com/rd4gkV

In case of <b> tag in resourses string you could wrap text in <![CDATA[ and ]]> , ie:

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

Then you could get and show it so:

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

<strong> HTML tag is not a "styling" tag. It's here only to indicates that the content is important. The default style of the <strong> relies on the web engine implementation.

You can have some information on these two links :

Try to use <b> instead of <strong> if you want a bold text.

you can do this several ways like

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

Example :

using strong tag

Deneme must be bold kayıt.

using b tag

Deneme kayıt

Try this.

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

You can also do same thing from XML by useing of HTML tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM