RuntimeException: native typeface cannot be made or memory leak for custom TextView loading font

后端 未结 4 1295
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 08:07

There\'s a HUGE problem in my code wherein I am loading a font in my assets\\fonts\\ folder from a custom TextView class. The first problem is that

4条回答
  •  一个人的身影
    2021-01-03 08:36

    If you are extending this view from xml then try using it this way::

    public class MyTextView extends TextView {
    
      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          init();
      }
    
     public MyTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
    
     public MyTextView(Context context) {
          super(context);
          init();
     }
    
    
    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/hirakakupronbold.ttf");
        setTypeface(tf);
    
    }
    

    }

    Its working fine for me. Make separate class extending TextView for each typeface style.to To apply it, replace your "TextView" with "com.yourpackage.MyTextView"

    Regards,

提交回复
热议问题