简体   繁体   English

确保已在Android上加载嵌入式字体资源

[英]Make sure embedded font asset has loaded on Android

So I have a font file in my assets folder for a android application. 所以我在我的Assets文件夹中有一个用于Android应用程序的字体文件。 To load it into the view I am calling it like so: 要将其加载到视图中,我这样调用它:

Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/my-font.otf");
textView.setTypeface(typeface);

And this works great, however on one device I am testing with it does not load the font, and when trying to set the typeface like this it does not cause any error. 效果很好,但是在我测试的一台设备上,它不会加载字体,并且在尝试设置这种字体时,不会引起任何错误。 I was wondering if anyone knew of a way to check if the font file has been loaded? 我想知道是否有人知道一种检查字体文件是否已加载的方法?

One thing that is worth mentioning is the device I'm talking about is currently an Xperia Play running cynanogen mod. 值得一提的是,我正在谈论的设备目前是运行cynanogen mod的Xperia Play。 Is it possible that it's the mod that is causing this issue? 可能是Mod导致了此问题? Every other device I've tested on has been fine however I only have the one which runs CM. 我测试过的所有其他设备都很好,但是我只有运行CM的设备。

Also, the minimum SDK is 14. 另外,最低SDK为14。

Try this: 尝试这个:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle     savedInstanceState) {
       View v = inflater.inflate(R.layout.fragment_layout, container, false);
       TextView txt = (TextView) v.findViewById(R.id.YourTextView);
       Typeface fontstyle = Typeface.createFromAsset(getActivity().getAssets(), "fonts/my-font.otf");
       txt.setTypeface(fontstyle); 
       return v;
}

可能有点晚了,但是请尝试从otf文件的名称中删除“-” ...“ fonts / my-font.otf”应为“ fonts / my_font.otf”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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