How to change the font on the TextView?

前端 未结 16 1231
清歌不尽
清歌不尽 2020-11-22 08:09

How to change the font in a TextView, as default it\'s shown up as Arial? How to change it to Helvetica?

16条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 08:42

    Maybe something a bit simpler:

    public class Fonts {
      public static HashSet fonts = new HashSet<>();
    
      public static Typeface get(Context context, String file) {
        if (! fonts.contains(file)) {
          synchronized (this) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);
            fonts.put(name, typeface);
          }
        }
        return fonts.get(file);
      }
    }
    
    // Usage
    Typeface myFont = Fonts.get("arial.ttf");
    

    (Note this code is untested, but in general this approach should work well.)

提交回复
热议问题