关于android自定义字体
我想自行定义内容的字体,用了以下语句:
TextView tv = (TextView)findViewById(R.id.maintitle);
//换字体
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HYyy.ttf");
tv.setTypeface(face);
在assets/front里导入了HYyy.ttf这个字体文件,大小是3.37M
但程序不知道为什么运行不了
出现的logcat如下:
04-01 02:33:21.338: E/AndroidRuntime(323): FATAL EXCEPTION: main
04-01 02:33:21.338: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{mars.product/mars.product.mainActivity}: java.lang.RuntimeException: native typeface cannot be made
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 02:33:21.338: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 02:33:21.338: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 02:33:21.338: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 02:33:21.338: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 02:33:21.338: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
04-01 02:33:21.338: E/AndroidRuntime(323): Caused by: java.lang.RuntimeException: native typeface cannot be made
04-01 02:33:21.338: E/AndroidRuntime(323): at android.graphics.Typeface.<init>(Typeface.java:147)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-01 02:33:21.338: E/AndroidRuntime(323): at mars.product.mainActivity.onCreate(mainActivity.java:42)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 02:33:21.338: E/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 02:33:21.338: E/AndroidRuntime(323): ... 11 more
请问需要如何修改?
[解决办法]
代码没有问题,3M+太大了,用 FontCreator 裁剪一下试试看,几百K。
我也是猜这有可能是native typeface cannot be made 的原因,试试吧
[解决办法]
把文件格式定义为mp3或者zip,这样就没有大小限制了,使用的时候就还是这么用,你试试