做项目的时候,需要使用到手写字体来让内容更加的美观。可是程序中默认使用的是系统的默认字体,怎么将TextView(或EditText)的字体设置成自己想要的字体呢?步骤如下:

  1、下载字体文件(.ttf格式),比如Jinglei.ttf(方正静蕾的字体文件),然后将其复制到项目工程的assets/fonts目录下。

  2、设置TextView的字体:

 TextView tv = (TextView)findViewById(R.id.my_textview);
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/Jinglei.ttf"); // mContext为上下文
tv.setTypeface(typeface );

  3、为了使用起来方便,还可以将设置字体的操作封装成一个工具类:

 /**
* 字体相关操作工具类
*
*/
public class TypefaceUtil {
// 上下文
private Context mContext;
private Typeface mTypeface; /**
* 如果ttfPath为null那么mTypeface就为系统默认值
*
* @param context
* @param ttfPath
*/ public TypefaceUtil(Context context, String ttfPath) {
mContext = context;
mTypeface = getTypefaceFromTTF(ttfPath);
} /**
* 从ttf文件创建Typeface对象
*
* @ttfPath "fonts/XXX.ttf"
*/
public Typeface getTypefaceFromTTF(String ttfPath) { if (ttfPath == null) {
return Typeface.DEFAULT;
} else {
return Typeface.createFromAsset(mContext.getAssets(), ttfPath);
}
} /**
* 设置TextView的字体
*
* @tv TextView对象
* @ttfPath ttf文件路径
* @isBold 是否加粗字体
*/
public void setTypeface(TextView tv, boolean isBold) {
tv.setTypeface(mTypeface);
setBold(tv, isBold);
} /**
* 设置字体加粗
*/
public void setBold(TextView tv, boolean isBold) {
TextPaint tp = tv.getPaint();
tp.setFakeBoldText(isBold);
} /**
* 设置TextView的字体为系统默认字体
*
*/
public void setDefaultTypeFace(TextView tv, boolean isBold) {
tv.setTypeface(Typeface.DEFAULT);
setBold(tv, isBold);
} /**
* 设置当前工具对象的字体
*
*/
public void setmTypeface(String ttfPath) {
mTypeface = getTypefaceFromTTF(ttfPath);
} }

  4、使用的时候只需这样调用:

 TypefaceUtil tfUtil = new TypefaceUtil(mContext,"fonts/Jinglei.ttf");
tfUtil.setTypeface(tv,false);

随机推荐

  1. [原创]超强C#图片上传,加水印,自动生成缩略图源代码

    <%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ ...

  2. 40-语言入门-40-C小加之随机数

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=255   15 20 32 40 67 89 300 400   代码: #includ ...

  3. openWRT自学---基于backfire版本,分析其Make命令的执行过程和各阶段的主要产物

    准备阶段:从SVN下载backfire的编译环境(位置是:svn co svn://svn.openwrt.org/openwrt/branches/backfire),然后按照openWRT的要求, ...

  4. Python的实例方法,类方法,静态方法之间的区别及调用关系

    如果只看这个图,很多人可能会看的一头雾水,特别是学过完全面向对象语言的同学, Python 是双面向的,既可以面向函数编程,也可以面向对象编程,所谓面向函数就是单独一个. py 文件,里面没有类,全是 ...

  5. C# - Passing Reference-Type Parameters

    A variable of a reference type does not contain its data directly; it contains a reference to its da ...

  6. SHA信息摘要

    SHA算法是在MD4的基础上演进而来的,通过SHA算法能够获得一个固定长度的摘要信息.   SHA算法系列有SHA-1(也成为SHA),SHA-224,SHA-256,SHA-384和SHA-512这 ...

  7. Html--判断客户端类型

    公司安排做一个html的app下载页面,需要检测客户端,走不同的css布局,于是从网上搜点资料,简单汇总下,方便日后查阅. 1) 响应式布局设置--@media only screen and onl ...

  8. Spring4 MVC REST服务使用@RestController实例

    在这篇文章中,我们将通过开发使用 Spring4 @RestController 注解来开发基于Spring MVC4的REST风格的JSON服务.我们将扩展这个例子通过简单的注释与JAXB标注域类支 ...

  9. Classification week6: precision & recall 笔记

    华盛顿大学 machine learning :classification  笔记 第6周 precision & recall 1.accuracy 局限性 我们习惯用 accuracy ...

  10. C字符串复制

    void mystrcpy(char *from, char *to) { for(; *from != '\0'; from++, to++) { *to = *from; } *to = '\0' ...