Java 在 Android Studio 中使用 otf 类型字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37301876/
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-11 19:10:14  来源:igfitidea点击:

Using otf type fonts in Android studio

javaandroidandroid-studiofontswebfonts

提问by Ganga

I see a lot of references for ttf fonts in Android studio but none describe usage of otf type fonts. Is there a way to use otf type fonts as well for Android studio projects?

我在 Android Studio 中看到了很多关于 ttf 字体的参考资料,但没有描述 otf 类型字体的用法。有没有办法在 Android Studio 项目中使用 otf 字体?

回答by Ahmad Aghazadeh

TTF is always been supported. OTF was later added at least in 1.6 and later, but partially.

一直支持 TTF。OTF 后来至少在 1.6 及更高版本中添加,但部分添加。

Typeface face;

face = Typeface.createFromAsset(getAssets(), "font.otf");

textview.setTypeface(face);

Helpful link : Use external fonts in android

有用的链接:在 android 中使用外部字体

You can convert this page : http://www.ehow.com/how_6192479_convert-dfont-ttf.html, https://onlinefontconverter.com/

您可以转换此页面:http: //www.ehow.com/how_6192479_convert-dfont-ttf.html,https: //onlinefontconverter.com/

回答by Nikhil Biju

You can add external fonts by adding it to the assets folder.

您可以通过将其添加到资产文件夹来添加外部字体。

The Typefaceclass specifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally Paint settings like textSize, textSkewX, textScaleX to specify how text appears when drawn (and measured).

字样类指定字体的字体和内在风格。这用于绘制,以及可选的绘制设置,如 textSize、textSkewX、textScaleX,以指定绘制(和测量)时文本的显示方式。

So create an object with reference to the font places in assets folder and apply the font style by setTypeface()method.

因此,参考assets文件夹中的字体位置创建一个对象并通过setTypeface()方法应用字体样式。

Example:

例子:

TextView usernamesigin = (TextView)findViewById(R.id.username_signin_edittxt);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MyriadPro-Regular.otf");
usernamesigin.setTypeface(tf);