Android - Using Custom Font

后端 未结 21 1666
别跟我提以往
别跟我提以往 2020-11-22 04:38

I applied a custom font to a TextView, but it doesn\'t seems to change the typeface.

Here is my code:

    Typeface myTypeface = Typeface         


        
相关标签:
21条回答
  • 2020-11-22 05:29

    I've successfully used this before. The only difference between our implementations is that I wasn't using a subfolder in assets. Not sure if that will change anything, though.

    0 讨论(0)
  • 2020-11-22 05:31

    Unfortunately there is no good solution for this.

    I've seen the many articles about using a custom TextView but what they forget it that it's not only textviews that can implement fonts & there are textviews hidden away in other views inaccessible to the developer; I'm not even going to get started on Spannable.

    You could use an external font utility like:

    Calligraphy Font Tool

    BUT This loops over every view in the application on it's creation and even this utility misses some views (ViewPager renders normal font) then you have the problem that is when Google updates their build tools this will occasionally crash because it needs to target deprecated properties. It's also a little slow as it uses Java's Reflection.

    It's really up to Google to fix this. We need better font support in Android. If you look at the solution from iOS they literally have 100's of fonts built in to select from. Want a custom font? Simply drop a TFF in and it's usable..

    For now were now limited to the offering that Google offers us which is extremely limited but fortunately mobile optimized.

    0 讨论(0)
  • 2020-11-22 05:35

    For Custom Fonts in android create a folder within assets folder name it "fonts" place your desired fonts.ttf or .otf file in it.

    If you extends UIBaseFragment:

    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Arial.ttf");
            tv.setTypeface(font);
    

    else if extends Activity:

    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Arial.ttf");
            tv.setTypeface(font);
    
    0 讨论(0)
提交回复
热议问题