我有一个帮助方法,可以执行以下操作:
public CustomTypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
切换到可下载字体后,我们得到以下异常,因为assets文件夹中没有可用的字体。我们怎样才能用可下载的字体执行此操作?谢谢!
引起:java.lang.RuntimeException:找不到字体资源fonts / OpenSans-Regular.ttf 在android.graphics.Typeface.createFromAsset(Typeface.java:206) 在com.woot.util.ui.CustomTypefaceSpan。(CustomTypefaceSpan.java:19) 在com.woot.util.ui.ActivityUtil.setTitleCustomFont(ActivityUtil.java:20) 在com.woot.storelocator.selectstorelanding.SelectStoreActivity.onCreate(SelectStoreActivity.java:40) 在android.app.Activity.performCreate(Activity.java:6664) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:1)
以下示例代码说明了整个可下载字体过程:
FontRequest request = new FontRequest("com.example.fontprovider.authority",
"com.example.fontprovider", "my font", certs);
FontsContract.FontRequestCallback callback =
new FontsContract.FontRequestCallback() {
@Override
public void onTypefaceRetrieved(Typeface typeface) {
// Your code to use the font goes here
...
}
@Override
public void onTypefaceRequestFailed(int reason) {
// Your code to deal with the failure goes here
...
}
};
FontsContract.requestFonts(context, request, callback , handler);