我想从url设置typeType,如:https://github.com/anak10thn/frontend-homepage/raw/master/font/helvetica-regular.otf
你知道有什么办法吗?
我试过了:
Typeface tf = Typeface.createFromFile("https://github.com/anak10thn/frontend-homepage/raw/master/font/helvetica-regular.otf");
tv.setTypeface(tf);
它不起作用。
我有这个错误:
03-24 04:46:59.931 1314-1314/bya.fchps.buildyourapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bya.fchps.buildyourapp, PID: 1314
java.lang.RuntimeException: Unable to start activity ComponentInfo{bya.fchps.buildyourapp/com.fchps.buildyourapp.views.ClassicView}: java.lang.RuntimeException: native typeface cannot be made
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:175)
at android.graphics.Typeface.createFromFile(Typeface.java:159)
at com.fchps.buildyourapp.views.ClassicView.setClassicLabel(ClassicView.java:178)
at com.fchps.buildyourapp.views.ClassicView.onCreate(ClassicView.java:75)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
答案 0 :(得分:1)
我不相信你真的可以。自定义字体通常保存为系统资源文件,而不是称为Web文档。据我所知,最好的方法很可能是:在assets文件夹中创建一个fonts文件夹。下载您想要的字体并将其放在fonts文件夹中,然后在代码中返回您的fonts文件夹。有关详细说明,请参阅此网站:http://www.tutorialspoint.com/android/android_custom_fonts.htm
答案 1 :(得分:1)
您好,您不能直接在字体中添加字体的URL。如果要在应用程序中动态使用该字体,最好的选择是应用程序将在本地设备中下载第一个字体。然后在字体中定义字体的路径。
Typeface typeface = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory(), "font.ttf"));