我可以在 Android 应用程序中嵌入自定义字体吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3424165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:08:11  来源:igfitidea点击:

Can I embed a custom font in an Android application?

androidfonts

提问by Pentium10

I would like to have an app include a custom font for rendering text, load it, and then use it with standard elements like StaticText. Is this possible?

我想让一个应用程序包含一个用于呈现文本的自定义字体,加载它,然后将它与像 StaticText 这样的标准元素一起使用。这可能吗?

回答by Sephy

Yes you can, you jsut can't define it into xml layouts. You need to use it dynamically each time. Check this tutorialfor instance.

是的,您可以,但您不能将其定义为 xml 布局。您每次都需要动态使用它。例如检查本教程

In case link is dead, here is a sum up of the stuff :

如果链接已死,这里是内容的总结:

  • Get a font file like times.otf
  • Drop it in your asset folder, inside a "fonts" folder
  • Get a reference of TextView with something like that:

    TextView tv = (TextView) findViewById(R.id.myCustomTVFont);
    
  • Grab you font from the asset folder:

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/times.otf");
    
  • Make your TextView look great:

    tv.setTypeface(tf);
    
  • 获取一个字体文件,如 times.otf
  • 将它放在您的资产文件夹中,在“字体”文件夹内
  • 使用类似的东西获取 TextView 的引用:

    TextView tv = (TextView) findViewById(R.id.myCustomTVFont);
    
  • 从资产文件夹中获取字体:

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/times.otf");
    
  • 让你的 TextView 看起来很棒:

    tv.setTypeface(tf);
    

回答by Matt

As of Android 8.0 (API level 26), you can use fonts in XML. See the documentation here.

从 Android 8.0(API 级别 26)开始,您可以在 XML 中使用字体。请参阅此处的文档。

回答by SME

Your can take look in this threadas well to set custom fonts for all the views in your activity.

您也可以查看此线程,为活动中的所有视图设置自定义字体。