开发者

Can I embed a custom font in an Android application?

开发者 https://www.devze.com 2023-01-10 18:01 出处:网络
I would like to ha开发者_JAVA技巧ve an app include a custom font for rendering text, load it, and then use it with standard elements like StaticText. Is this possible?Yes you can, you jsut can\'t defi

I would like to ha开发者_JAVA技巧ve an app include a custom font for rendering text, load it, and then use it with standard elements like StaticText. Is this possible?


Yes you can, you jsut can't define it into xml layouts. You need to use it dynamically each time. Check this tutorial for instance.

In case link is dead, here is a sum up of the stuff :

  • Get a font file like times.otf
  • Drop it in your asset folder, inside a "fonts" folder
  • Get a reference of TextView with something like that:

    TextView tv = (TextView) findViewById(R.id.myCustomTVFont);
    
  • Grab you font from the asset folder:

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/times.otf");
    
  • Make your TextView look great:

    tv.setTypeface(tf);
    


As of Android 8.0 (API level 26), you can use fonts in XML. See the documentation here.


Your can take look in this thread as well to set custom fonts for all the views in your activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消