如何定义使用自定义字体样式 [英] How to define style that use custom font

查看:216
本文介绍了如何定义使用自定义字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的Roboto字体作为我的应用程序,每TextViews,EditTexts,按钮的默认字体,等等。

I would like to use Roboto font as default font for every TextViews, EditTexts, Buttons, etc in my app.

我已经把在字体文件TTF文件夹内的资产的文件夹。现在我想编辑的应用风格,才能使用该字体。
所以,这就是我所做的。

I've put the ttf file in the fonts folder inside assets folder. Now I would like to edit the app style, in order to use that font. So, that's what I've done.

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:typeface">ROBOTO-REGULAR.TTF</item>
</style>

但是,编译器返回此错误。

But the compiler returns this error.

String types not allowed (at 'android:typeface' with value 'ROBOTO-
 REGULAR.TTF').

是否有可能通过XML定义应用程序默认字体?在这种情况下,有什么不好?
谢谢你在前进。

Is it possible to define via XML a default font for the app? In this case, what is wrong? Thank you in advance.

推荐答案

您不能做到这一点,你想要的方式。在的android:字体属性是一个枚举,并具有固定的值数。它并不需要一个文件名。

You can't do it the way you want. The android:typeface attribute is an enum and has a fixed number of values. It doesn't take a filename.

您的能有什么的做的就是实现一个自定义的的TextView (加上定制按钮的EditText 从各自的类继承)读取自定义属性,并加载字体文件的属性点。

What you can do is implement a custom TextView (plus custom Button and EditText inheriting from their respective classes) that reads a custom attribute and loads the font file that the attribute points to.

时,要注意的Roboto是为了从蜂巢开始使用(或者是它ICS?)。它看起来有点格格不入的旧设备,其中德罗伊德桑是系统范围内的默认。

Bear in mind that Roboto is meant to be used from Honeycomb onwards (or was it ICS?). It does look a bit out of place on older devices, where Droid Sans is the system-wide default.

这篇关于如何定义使用自定义字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆