Html in text view with different fonts for bold and italic

前端 未结 4 1268
忘掉有多难
忘掉有多难 2021-01-05 03:46

I\'m trying to use a custom font on a TextView. The TextView text is set with textView1.setText(Html.fromHtml(htmlText));

The

4条回答
  •  我在风中等你
    2021-01-05 04:15

    The best way right now for API 16+ is to define a font resource file if you are using Support Library above v26 or the new AndroidX libraries, basically you add your normal and italic ttf font files in the fonts folder and create a font resource xml and basically make it look something like

    
    
    
    
    
    
    

    the last one is for bold fonts, apply this xml suppose custom_font_family.xml to your textview as android:fontFamily="@font/custom_font_family", now any html text you set with fromHtml with any of the three span types will use the proper fonts, when needed, this allows you to even have a custom font family mixing entirely different fonts and doesn't quite literally have to be from the same family.

提交回复
热议问题