无法为4.0.3-4.0.4制作本机字体

时间:2014-04-01 15:24:31

标签: android

我刚刚向应用程序商店推出了一个应用程序,并且我收到了有关为我自定义TextView充气的错误,我用它来实现自定义字体。这些错误似乎来自运行4.0.3或4.0.4的人,并且它在我的S3运行4.3以及Nexus 7上工作正常。是否有一些我缺少使其兼容的东西?我知道this SO question有同样的问题,但并没有完全解决它。我添加了一个try / catch以将其默认恢复为内置字体,但似乎应该有一个解决方案,所以我可以使用我的自定义字体。

logcat的:

Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:586)
... 22 more
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:430)
at android.graphics.Typeface.createFromAsset(Typeface.java:404)
at com.gtf.oneparish.AvTextView.<init>(AvTextView.java:18)
... 25 more

AvTextView:

public class AvTextView extends TextView {
    public static Typeface FONT_NAME;


    public AvTextView(Context context) {
        super(context);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
    public AvTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
    public AvTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

似乎Android 4.0.x对字体有点挑剔,并且无论出于何种原因拒绝加载其中一些字体。通过在FontForge中调整其字体信息(以更接近地匹配另一个成功加载的字体)并生成新的.ttf文件,我能够加载我的自定义字体。原始.ttf在我的计算机和Androids 2.3.x,4.3和4.4上工作正常,但在4.0.3和4.0.4中引发异常。