如何在Android中添加替代字体到Button文本?

时间:2012-08-29 13:36:20

标签: java android button custom-font

我正在尝试在我的一个Android应用程序中的按钮上使用Calibri字体,在搜索了一下之后我发现了如何制作自己的Typeface。但是,当我尝试在按钮上设置字体时,它不会给我一个错误,但应用程序将无法启动。   有谁知道如何解决这个问题?

final Button bt4 = (Button) findViewById(R.id.bt4);
final Typeface font = Typeface.createFromAsset(getAssets(),"CALIBRI.ttf");
bt4.setTypeface(font);

这几乎是我在这一点上使用的所有代码。

和日志猫信息

08-29 13:31:20.593: E/global(1143): Deprecated Thread methods are not supported.
08-29 13:31:20.593: E/global(1143): java.lang.UnsupportedOperationException
08-29 13:31:20.593: E/global(1143):     at java.lang.VMThread.stop(VMThread.java:85)
08-29 13:31:20.593: E/global(1143):     at java.lang.Thread.stop(Thread.java:1280)
08-29 13:31:20.593: E/global(1143):     at java.lang.Thread.stop(Thread.java:1247)
08-29 13:31:20.593: E/global(1143):     at com.github.TheCad.nijmegen1.SplashScreen$1.run(SplashScreen.java:33)
08-29 13:31:21.553: D/AndroidRuntime(1143): Shutting down VM
08-29 13:31:21.553: W/dalvikvm(1143): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 13:31:21.583: E/AndroidRuntime(1143): FATAL EXCEPTION: main
08-29 13:31:21.583: E/AndroidRuntime(1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.TheCad.nijmegen1/com.github.TheCad.nijmegen1.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.os.Looper.loop(Looper.java:123)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at java.lang.reflect.Method.invoke(Method.java:507)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at dalvik.system.NativeStart.main(Native Method)
08-29 13:31:21.583: E/AndroidRuntime(1143): Caused by: java.lang.RuntimeException: native typeface cannot be made
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.graphics.Typeface.<init>(Typeface.java:147)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.graphics.Typeface.createFromAsset(Typeface.java:121)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at com.github.TheCad.nijmegen1.MainActivity.onCreate(MainActivity.java:25)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-29 13:31:21.583: E/AndroidRuntime(1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-29 13:31:21.583: E/AndroidRuntime(1143):     ... 11 more

2 个答案:

答案 0 :(得分:1)

您已在RunTime创建TypeFace。

示例:

Button btn=(Button)findViewById(R.id.BUTTON_ID);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/FONT_NAME.TTF");
btn.setTypeface(face);

<强>更新 您在第25行主要活动时出错。 张贴那条线。

答案 1 :(得分:0)

在assets文件夹中创建一个名为“font”的文件夹,并将字体“calibri.ttf”存储在该文件夹中,同时尝试用小写字母保存字体名称并使用此代码,

Button btn=(Button)findViewById(R.id.BUTTON_ID);
Typeface face= = Typeface.createFromAsset(getAssets(), "font/calibri.ttf");
btn.setTypeface(face);