如何在Android Studio中使用自定义字体

时间:2017-04-23 22:22:32

标签: android fonts

我正在尝试在Android应用中使用自定义字体。我将我的字体文件放在/ app / src / main / assets / fonts下,然后用

引用它们
Typeface.createFromAsset(getApplicationContext().getAssets(), 
"fonts/magorian_font.ttf")

当我运行我的应用程序时,我收到错误:

04-23 16:14:40.017 13342-13342/com.pottermore.mandrake.pottermoreapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.pottermore.mandrake.pottermoreapp, PID: 13342
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pottermore.mandrake.pottermoreapp/com.pottermore.mandrake.pottermoreapp.ActivityHome}: java.lang.RuntimeException: Font asset not found magorian_font.ttf
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                    Caused by: java.lang.RuntimeException: Font asset not found magorian_font.ttf
                                                                                       at android.graphics.Typeface.createFromAsset(Typeface.java:206)
                                                                                       at com.pottermore.mandrake.pottermoreapp.ActivityHome.onCreate(ActivityHome.java:34)
                                                                                       at android.app.Activity.performCreate(Activity.java:6679)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我做错了什么?

2 个答案:

答案 0 :(得分:1)

在此路径中添加资源文件夹:src/main/assets/font

并使用此代码:

    Typeface typrface=Typeface.createFromAsset(getAssets(),"font/iranian_sans");

Text.setTypeface(typrface);

<强>更新

不要忘了,你必须在字体文件夹中复制ttf字体格式

答案 1 :(得分:-1)

当你想给TextView一个不同的字体时,它应该是这样的:)

private TextView yourtext;

yourtext= (TextView) findViewById(R.id.the_id_of_the_textview);
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/magorian_font.ttf");
    yourtext.setTypeface(tf);