​ 安卓开发使用自定义字体应该已经是司空见惯了,今天就来介绍一下如何使用自定义字体的方法。

准备工作

如下图,首先创建一个assets文件夹,然后新建一个fonts子文件夹,放入字体文件。

微信截图_20170816115004.png

方法一:通过重写TextView实现在xml中配置

1.重写TextView

private Context mContext;

private String TypefaceName = "";

public String getTypefaceName() {

return TypefaceName;

}

public void setTypefaceName(String typefaceName) {

TypefaceName = typefaceName;

Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

System.gc();

}

public FontTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

this.mContext = context;

int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);

if (resouceId != 0) {

TypefaceName = context.getResources().getString(resouceId);

} else {

TypefaceName = attrs.getAttributeValue(null, "typefaceName");

}

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

public FontTextView(Context context, AttributeSet attrs) {

super(context, attrs);

this.mContext = context;

// 先判断是否配置的资源文件

int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);

if (resouceId != 0) {

TypefaceName = context.getResources().getString(resouceId);

} else {

TypefaceName = attrs.getAttributeValue(null, "typefaceName");

}

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

public FontTextView(Context context) {

super(context);

this.mContext = context;

// TypefaceName = attrs.getAttributeValue(null, "TypefaceName");

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

}

2.xml使用typefaceName属性引用字体文件

android:layout_width="60dp"

android:layout_height="50dp"

android:textSize="20sp"

typefaceName="Simhei"

android:text="This is first child view"/>

方法二:代码内部设置

加载字体资源文件:

需要注意的是,字体文件加载不要放在主线程中,会阻塞线程,影响性能

public static Typeface typeFace =Typeface.createFromAsset(getContext().getAssets(),

"fonts/Simhei.ttf");

public static Typeface typeFaceArial= Typeface.createFromAsset(getContext().getAssets(),

"fonts/Arial.ttf");

使用:

TextView.setTypeface(typeFace);

是不是很简单了,两种方法任意选择。

android字体文件制作教程,Android使用自定义字体相关推荐

  1. java加载字体文件_Java的加载自定义字体文件(.TTF)

    我在下面这段代码中使用,并将其与该堆栈跟踪出现:Java的加载自定义字体文件(.TTF) java.io.FileNotFoundException: font.ttf (No such file o ...

  2. android bp文件_Android 基础 | Android.bp 语法浅析

    相关推荐: 随着 android 工程越来越大,包含的 module 越来越多,以 makefile 组织的项目编译花费的时间越来越多.Google 在 7.0 引入了 ninja 是 Google ...

  3. 生信技能04 - 生信分析所需致病SNP位点Excel文件制作教程

    致病SNP位点Excel文件制作教程 在生信分析项目中,有遗传相关的项目会涉及到SNP位点的查找,将该SNP位点文件作为程序输入文件,本文将介绍如果通过NCBI查看目标基因的致病SNP位点,并制作成可 ...

  4. html怎么设置毛笔字体,三种实用的毛笔书法字体设计制作教程

    三种实用的毛笔书法字体设计制作教程 书法字体2015.09.08书法字体 毛笔字.书法字都非常漂亮,但现在除了少数书法家,书法爱好者能写的一手漂亮的书法外,很少有人能写好书法字了!作为设计师(特别是网 ...

  5. ArcGIS使用字体文件制作符号库!

    ArcGIS使用字体文件制作符号库! ArcGIS制作的符号库有多种,刚刚毕业进入公司,接触到符号库的制作,在前人的脚步之下,本人整理通过字体符号(Character Marker Symbol)制作 ...

  6. IOS描述文件制作教程​

    转载:IOS描述文件制作教程 iOS描述文件(.mobileprovision)一键申请 在主界面上点击描述文件按钮. ​ 编辑切换为居中 添加图片注释,不超过 140 字(可选) 新建ios描述文件 ...

  7. 单反DCP文件制作教程 电影色调3Dlut快速实现

    单反DCP文件制作教程 dcptool下载地址:https://sourceforge.net/projects/dcptool/ 1.解压 2.快捷键win + r启动命令行 3.cd /d E:\ ...

  8. android开机动画视频教程,【Android开机动画制作教程】开机动画文件组成及其详解释疑!...

    [开机动画位置] 安卓原版系统开机动画位置: system/media/bootanimation.zip或者data/local/bootanimation.zip 这两处的区别在于动画放在syst ...

  9. androidstudio使用mac字体_如何在Android Studio编写的项目中使用自定义字体

    有很多方法可以在字段上设置自定义字体系列,我使用如下所示. 要将字体添加为资源,请在Android Studio中执行以下步骤: 1)右键单击res文件夹,然后转到新建> Android资源目录 ...

  10. android 镜像文件制作

    一:update.zip包的制作    1:新建一个目标,在此目录下准备好需要的文件,如system目录文件.boot.img.recovery.img等.      mkdir testupdate ...

最新文章

  1. Javascript Java C++系列
  2. 【AI】Win10-Tensorflow
  3. STM32HAL库---串口中断接收时间管理机制
  4. python 序列解包(解压缩)
  5. 让程序在崩溃时体面的退出之SEH
  6. c语言 连通域算法 递归,VC++ 6.0编写计算机图形学中的种子填充算法,想用递归的八向连通域,求助!...
  7. JavaScript的面向对象原理之原型链
  8. 运算符重载为类的友元函数
  9. VMWare——window server 2008/2012 administrator 密码忘记,设置重置密码
  10. 继承(引用~析构~virtual)
  11. log4j教程 11、日志记录到文件
  12. 性能测试--jmeter中参数化【14】
  13. 深度解析脑机接口技术的现状与未来!
  14. 蚂蚁金服十年自研分布式中间件,成就世界级新金融科技平台
  15. android视频采集
  16. methodinvocationexception: property ‘driverclassname‘ threw exception; nested exception is java.lang
  17. html屏幕遮挡,遮挡屏幕怎么设置 为什么手机上会出现请勿遮挡屏幕顶端
  18. 无法将为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型
  19. 网页设计配色应用教程之各种色彩对比分析
  20. android获取用户手机信息,Android – 使用AccountManager /手机所有者的姓氏和姓氏获取用户数据...

热门文章

  1. 根据线索整理的一套在2021年继续使用Flash Player的方法(20.12.29更新)
  2. swift野梦抄袭 taylor_如何看待蔡健雅新歌《半途》被指抄袭 Taylor Swift 的《Safe Sound》?...
  3. STL CSB 11.10
  4. 兔子如何吃掉狼和野猪的
  5. 【安全】如何关闭Windows系统的137, 139, 445端口?
  6. 计算机基础及Python简介
  7. 计算机无法找到输出设备,老司机搞定win10声音无法找到输入输出设置的解决方法...
  8. 怎么找到电脑的打印机
  9. 安徽省对口计算机试题答案,安徽省对口高考试卷
  10. word页眉页脚,奇偶不同