简体   繁体   中英

custom font in android by using typeface

i want to customize font style, so following is my code

  Typeface font = Typeface.createFromAsset(getAssets(), "Modernism-Disco.ttf");
                    date.setTypeface(font);
                    date.setText("my Name");

this shows the following error

java.lang.RuntimeException: native typeface cannot be made
            at android.graphics.Typeface.<init>(Typeface.java:175)
            at android.graphics.Typeface.createFromAsset(Typeface.java:149)
            at com.centricsol.org.awesomtorch.MainActivity$10.run(MainActivity.java:577)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

and my android studio doesn't show assets directory but it actually exists

在此处输入图片说明

As per your second picture you got assets folder inside res folder. This is incorrect location and you need to move your assets one level up to make it work:

assets/
java/
res/
AndroidManifest.xml

将您的资产文件夹移动到您的项目目录,以便res和资产文件夹在同一目录中。

There are basically 4 things that can cause this:

1)You use the wrong extension

2)You placed the fonts in the assets folder and not inside assets/fonts/

3)You misspelled the fonts

4)The fonts need to be lowercase (in your case the solution is to rename Modernism-Disco.ttf to modernism-disco.ttf, strange)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM