Android系统默认字体支持四种字体,分别为:
- noraml (普通字体,系统默认使用的字体)
- sans(非衬线字体)
- serif (衬线字体)
- monospace(等宽字体)
除此之外还可以使用其他字体文件(*.ttf)
关于后三种字体的区别可以看:
http://kb.cnblogs.com/page/192018/
一、使用系统自带的字体
1.在xml中修改字体
<!-- 使用默认的sans字体-->
<TextView
android:id="@+id/sans"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="sans" />
<!-- 使用默认的serifs字体-->
<TextView
android:id="@+id/serif"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="serif" />
<!-- 使用默认的monospace字体-->
<TextView
android:id="@+id/monospace"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="monospace" />
2.在java代码中修改字体
第一步:设置字体
//设置serif字体 textView.setTypeface(Typeface.SERIF); //设置sans字体 textView.setTypeface(Typeface.SANS_SERIF); //设置monospace字体 textView.setTypeface(Typeface.MONOSPACE);
二、在Android中可以引入其他字体
第一步:在assets目录下新建fonts目录,把ttf字体文件放到这,如图所示

第二步:程序中调用
//实例化TextView
TextView textView = findViewById(R.id.textview);
//得到AssetManager
AssetManager mgr=getAssets();
//根据路径得到Typeface
Typeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");
//设置字体
textView.setTypeface(tf);
如果是自定义控件(我们再paint 里面设置)paint.setColor(mtextColor);
paint.setTextSize(mtextSize);
paint.setStrokeWidth(0);
//设置字体
paint.setTypeface(Typeface.DEFAULT);
本文介绍了如何在Android系统中使用默认的四种字体(normal、sans、serif、monospace),并详细说明了如何引入和使用自定义字体文件。通过XML配置和Java代码两种方式展示了字体的应用方法。
139

被折叠的 条评论
为什么被折叠?



