使用自定义字体时出现问题 - “无法制作原生字体”

发布于 2024-12-06 05:22:36 字数 436 浏览 0 评论 0原文

我正在尝试使用在互联网上找到的字体,但问题是我得到的 FC 带有“无法制作原生字体”。

这是我的 ListVIew 的 getView 中的代码:

        holder.tv_SuraName   =(TextView)convertView.findViewById(R.id.Start_Name);
        holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "suralist_font.ttf"));

谁能告诉我为什么可以使用自定义 rom?您可以在此处获取它..该文件是.ttf

I'm trying to use a font I found on the internet, but the problem is that I get an FC with "native typeface cannot be made".

Here is the code in the getView of my ListVIew:

        holder.tv_SuraName   =(TextView)convertView.findViewById(R.id.Start_Name);
        holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "suralist_font.ttf"));

Can anyone tell me why can I use the custom rom? You can get it HERE .. the file is .ttf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

温柔一刀 2024-12-13 05:22:36

我的问题是资产文件夹的位置不正确。

使用 Android Studio 时,资产文件夹应位于源集内,例如

src/main/资产/

发现于这个答案

My problem was incorrect placement of the assets folder.

When using Android Studio the assets folder should be inside of the source sets e.g.

src/main/assets/

Found in this answer

何以畏孤独 2024-12-13 05:22:36

字体文件已损坏或由于某种原因不受支持。您可以将其放在 SD 卡上并从文件中加载,以确保您的资产不会出现问题。

The font file is either corrupt or unsupported for some reason. You can drop it on the SD card and load it from file, to make sure it's not a problem with your assets.

空城缀染半城烟沙 2024-12-13 05:22:36

基本上有 4 种情况可能会导致这种情况:

  1. 您使用了错误的扩展名
  2. 您必须将字体放在 asset 文件夹中,而不是放在 asset/fonts/ 中
  3. 您拼错了字体
  4. 字体需要小写(就我而言,解决方案是将 MyFont.ttf 重命名为 myfont.ttf,奇怪)

There are basically 4 things that can cause this:

  1. You use the wrong extension
  2. You have to place the fonts in the assets folder and not inside assets/fonts/
  3. You misspelled the fonts
  4. The fonts need to be lowercase (in my case the solution was to rename MyFont.ttf to myfont.ttf, strange)
你与昨日 2024-12-13 05:22:36

仔细检查扩展名是否大写,即。 suralist_font.TTF

字体通常看起来是这样的,但它可能会被忽视。

Double check the extension isn't in caps, ie. suralist_font.TTF

Fonts often come seem to come that way and it might be overlooked.

兲鉂ぱ嘚淚 2024-12-13 05:22:36

我遇到了同样的问题,我在 asset>fonts>whatever.ttf 中创建了我的问题,但遇到了相同的错误。我添加了字体扩展(fonts/whatever.ttf),它解决了每种情况下的问题。

 holder.tv_SuraName   =(TextView)convertView.findViewById(R.id.Start_Name);
 holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(),    "fonts/suralist_font.ttf"));

I had this same problem, I created mine in assets>fonts>whatever.ttf and was getting the same error. I added the fonts extension (fonts/whatever.ttf) and it fixed the problem in every case.

 holder.tv_SuraName   =(TextView)convertView.findViewById(R.id.Start_Name);
 holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(),    "fonts/suralist_font.ttf"));
岁月无声 2024-12-13 05:22:36

相当旧的线程,但我想添加另一个可能发生这种情况的原因:您必须在调用字体之前设置内容视图。请参阅下面的示例

是否不起作用

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView title = (TextView)findViewById(R.id.tvTitle);
        Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
        title.setTypeface(font);

        setContentView(R.layout.main);
    }

起作用!

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // <-- call before typeface

        TextView title = (TextView)findViewById(R.id.tvTitle);
        Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
        title.setTypeface(font);
    }

Rather old thread, but I want to add one more reason this can happen: You have to set the content view before calling typeface. See example below

Does not work:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView title = (TextView)findViewById(R.id.tvTitle);
        Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
        title.setTypeface(font);

        setContentView(R.layout.main);
    }

Does work!

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // <-- call before typeface

        TextView title = (TextView)findViewById(R.id.tvTitle);
        Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
        title.setTypeface(font);
    }
满天都是小星星 2024-12-13 05:22:36

我正在使用 android studio,在我的例子中,问题出在资产文件夹的路径上,在 Eclipse 中资产文件夹的路径是 res/assets/ 但在 Android Studio 中它是 src/main/assets/
所以,我只是将我的字体文件移动到这个路径,最终问题解决了。

I am using android studio, in my case problem was with the path of assets folder, in Eclipse path for assets folder is res/assets/ but in Android Studio its src/main/assets/
So, i just move my font file to this path and finally problem solved.

笑饮青盏花 2024-12-13 05:22:36

要么您的字体文件已损坏,要么您在调用文件名时犯了一些拼写错误。我遇到了同样的问题,这是因为资产文件夹中指定的文件名是

roboto_black.ttf

并在 java 文件中声明时,我将其拼写为:

roboto_blak.ttf

如果您的 .ttf 文件位于 asset 文件夹中,则使用:

Typeface type_bold = Typeface.createFromAsset(getAssets(), "roboto_black.ttf");

否则如果它位于 fonts 文件夹中,则使用:

type_bold = Typeface.createFromAsset(getAssets(), "fonts/roboto_black.ttf");

希望它有帮助!

Either your font file is corrupt else you making some mistake in spelling while calling your file name. I had same issue, it was because file name specified in asset folder was

roboto_black.ttf

and while declaring in java file, i have been spelling it as:

roboto_blak.ttf

If your .ttf files are in asset folder,then use:

Typeface type_bold = Typeface.createFromAsset(getAssets(), "roboto_black.ttf");

Else if it is in fonts folder, then use:

type_bold = Typeface.createFromAsset(getAssets(), "fonts/roboto_black.ttf");

Hope it helps!

木落 2024-12-13 05:22:36

希望这将有助于

Typeface.createFromAsset 泄漏资源流:http://code. google.com/p/android/issues/detail?id=9904

Hope this will help

Typeface.createFromAsset leaks asset stream: http://code.google.com/p/android/issues/detail?id=9904

七分※倦醒 2024-12-13 05:22:36

正如 CommonsWare 在这个答案中所述

我猜是字体本身有问题。那个错误
当操作系统中的本机代码尝试加载
字体。

不管怎样,试试这个:

holder.tv_SuraName = (TextView)convertView.findViewById(R.id.Start_Name);
Typeface Font = Typeface.createFromAsset(this.getAssets(),"suralist_font.ttf");
holder.tv_SuraName.setTypeface(Font);

As CommonsWare states in this answer

I would guess that there is a problem with the font itself. That error
will be triggered when native code in the OS attempts to load the
typeface.

Anyway try this one:

holder.tv_SuraName = (TextView)convertView.findViewById(R.id.Start_Name);
Typeface Font = Typeface.createFromAsset(this.getAssets(),"suralist_font.ttf");
holder.tv_SuraName.setTypeface(Font);
沫尐诺 2024-12-13 05:22:36

我对来自Windows 的comic sans ms 的COMIC.TTF 也有同样的问题。
由于我放置在 asset/fonts 文件夹中的原始文件如上面拼写的那样是大写的,因此我必须完全以大写形式输入它。
Typeface.createFromAsset(getAssets(), "fonts/COMIC.TTF");

事实证明问题是由小写引起的:
Typeface.createFromAsset(getAssets(), "fonts/comic.ttf");

I had the same problem with COMIC.TTF from comic sans ms of Windows.
Since the original file I placed in assets/fonts folder was in uppercase as spelt above, I had to type it exactly in uppercase.
Typeface.createFromAsset(getAssets(), "fonts/COMIC.TTF");

it turns out the problem was caused by the lower case:
Typeface.createFromAsset(getAssets(), "fonts/comic.ttf");

岁月苍老的讽刺 2024-12-13 05:22:36

请注意,这是由于文件损坏或您将该文件放置在错误的文件夹中造成的。

根据Android文档此处

主要/资产/
这是空的。您可以使用它来存储原始资源文件。您在此处保存的文件将按原样编译为 .apk 文件,并保留原始文件名。您可以使用 URI 以与典型文件系统相同的方式导航此目录,并使用 AssetManager 将文件作为字节流读取。例如,这是纹理和游戏数据的好位置。

因此,您的文件夹结构应该是 yourmodule/main/assets (例如 a***pp/main/assets*** )

Please note that this is due to either a corrupt file or you have placed the file in the wrong folder.

According to Android documentation here

main/assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

Therefore your folder structure should be yourmodule/main/assets (e.g. a***pp/main/assets*** )

终止放荡 2024-12-13 05:22:36

对我有用的是首先将我的字体名称更改为仅小写和下划线:从 Amatic-Sc.ttfamatic_sc.ttf

然后在 set.TypeFace 方法中将 assets/fonts/amatic_sc.ttf 更改为 fonts/amatic_sc.ttf

What worked for me was first changing my font name to only lower case and underscore: from Amatic-Sc.ttf to amatic_sc.ttf.

Then in the set.TypeFace method change assets/fonts/amatic_sc.ttf to just fonts/amatic_sc.ttf.

一页 2024-12-13 05:22:36

我一直在 Android Studio 中调试这个错误。将字体添加到资产目录后清理并重建项目为我解决了这个问题。

I've been debugging forever on this error in Android Studio. Clean and rebuilding the project after adding the fonts to the assets directory fixed the issue for me.

近箐 2024-12-13 05:22:36

刚刚做了一个构建 --> clean 并开始渲染。

Just did a build --> clean and it started rendering .

撩人痒 2024-12-13 05:22:36

我在 Android 4.0.3-4.0.4 上遇到了同样的问题(在模拟器 IceCreamSandwich mips 4.0.3 上可 100% 重现)。

问题是我拥有的 TTF 字体不正确,尽管几乎所有 Android 版本都能够加载这些 TTF 字体。所以我使用了 https://cloudconvert.com/woff-to-ttf (因为我有woff 文件)来制作新的 TTF 字体,这解决了我的问题。

I had same issue which happened on Android 4.0.3-4.0.4 (100% reproducible on emulator IceCreamSandwich mips 4.0.3).

The problem was that TTF fonts, that I had, were not correct, despite almost all Android versions were able to load those TTF font. So I used https://cloudconvert.com/woff-to-ttf (since I had woff files as well) to make new TTF fonts, which solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文