Android中有三种默认的字体,但是我想要引入更多的字体
我们知道Android系统中有三种默认支持的字体,分别是“sans”, “serif”, “monospace"。但是有时候我们会想要引入其他的字体,所以这里介绍如何在Android中通过添加(*.ttf)文件来引入其他的字体,至于什么是ttf文件,TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式。
一:XML中使用android默认字体
<!-- 使用默认的sans字体,可以通过设置typeface属性来更改为serif或者monospace-->
<TextView Android:id="@+id/sans"
Android:text="Hello,World"
Android:typeface="sans"
Android:textSize="20sp" />
二:在Android中可以引入其他字体
1.首先要将字体文件保存在assets/fonts/目录下,注意在Android Studio下创建 assets文件,同 res文件同级别。
2.java程序中的工具类
public class TypefaceUtils { public static void setTypeface(Context context,TextView view,String path) { Typeface typeface=Typeface.createFromAsset(context.getAssets(), "fonts/ios9.ttf"); //使用字体 view.setTypeface(typeface); } }
然后将要设置字体的TextView传进来就可以了,ios9.ttf是我放入assets文件夹的font目录下的文件,你可以引入自己的字体文件。