create typeface from font resource id

喜夏-厌秋 提交于 2019-12-09 14:00:34

问题


I have tried using Typeface.createFromAsset(getAssets(),"font/myfont.ttf"));

I know font directory should be in assets but in my case, I have an existing font resource directory so I thought asset manager will read from font resource directory but I was wrong. I am creating Typeface to set custom font for collapsingToolbarLayout.

I found this answer but it requires me to keep font in assets


回答1:


This worked

Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);

Found this in the tutorial link by @Eselfar




回答2:


If you want to create the TypeFace from a font stored in assets folder,

you don´t need to define the directory, just the name of the font:

Typeface typeface =  Typeface.createFromAsset(getAssets(),"app_font.ttf");

You can create the Typeface using the resource id of the font stored into the /font folder like this :

Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);

your font must be stored into the /font directory



来源:https://stackoverflow.com/questions/48688026/create-typeface-from-font-resource-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!