在最近的工作中遇到了设置自定义字体的问题,经过在网上查询相关资料和看源码最终将自定义字体弄明白了,因此写下此博客记录一下,希望能给其他人一些帮助。
对比ios系统,Android中默认的字体在中文显示上是十分难看的,尤其是字号比较大的时候,默认字体样式都会感觉比较粗,所以一般对于产品有追求的设计,都会考虑换一套字体样式。在应用中需要配置不同的字体,而 Android 只能在 xml 中配置系统默认提供的四种字体,需要自定义的字体都需要在 Java 代码中配置

为什么自定义字体

Android系统默认使用的是一款叫Roboto的字体。如果你想要突出一个元素,那么会有很多的选择:颜色,大小,样式(粗体,斜体,普通),另一种方式就是使用不同于系统的字体来装饰你的view。

最快的使用自定义字体的方法

首先我们需要在网上找到自己要设置的字体文件 xxx.ttf 比较好的地方有1001 Free Fonts或者是Google Fonts。

然后把下载下来的xxx.ttf文件放到工程的asset/fonts路径下面,如果工程中没有这个路径可以自己建一个这样的路径 建立的方法如图所示

建立路径成功之后将xxx.ttf 文件放在该目录下面 如下图所示

最后就是将这个字体运用到你想要改变的 TextView 上面。

TextView textView = (TextView)findViewById(R.id.qy_w_subject_tv);
Typeface mCustomFont = Typeface.createFromAsset(getAssets(), "fonts/impact_custom.ttf");
textView.setTypeface(mCustomFont);

这样就结束了,如果想要改变一个Textview的字体就是这么简单,最好的情况就是上面的代码在 onCreate() 方法中进行调用。

在我的工程中就两处用到了自定字体,所以我就用了上面的方法去实现,如果很多地方都需要改变字体可以用下面的方法,下面的方法是从别人的文章中粘贴过来的 仅供参考

提供字体内存缓存

虽然Android现在已经很流畅,但是我们依然应该考虑优化性能。所以,我们应该把自定义的字体缓存起来,这样就不用每次去初始化 方法如下

public class FontCache {private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();public static Typeface get(String name, Context context) {Typeface tf = fontCache.get(name);if(tf == null) {try {tf = Typeface.createFromAsset(context.getAssets(), name);}catch (Exception e) {return null;}fontCache.put(name, tf);}return tf;}
}

缓存下字体就能让我们不用一直去操作 Assets 文件夹,接下来就能实现一个继承自 TextView 的类。
也就是所谓的自定义View

继承TextView

public class EatFoodyTextView extends TextView {public EatFoodyTextView(Context context) {super(context);applyCustomFont(context);}public EatFoodyTextView(Context context, AttributeSet attrs) {super(context, attrs);applyCustomFont(context);}public EatFoodyTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);applyCustomFont(context);}private void applyCustomFont(Context context) {Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);setTypeface(customFont);}
}

开始的三个都是构造函数,里面都调用了 applyCustomFont() 方法,然后从上面的 FontCache 类中拿到缓存的字体文件,这样就不用每个view都去重复的从 Assets中取字体,节约了资源,最后将取到的字体设置到 setTypeface() 中。

使用自定义类

现在我们只需要在XML中直接使用,不需要再写其他的java代码,

<RelativeLayout  android:layout_width="match_parent"android:layout_height="match_parent"><com.futurestudio.foody.views.EatFoodyTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/eat_foody_green_dark"android:textSize="20sp"android:text="Future Studio Blog"android:layout_marginBottom="24dp"/></RelativeLayout>

我们可以依然使用TextView的其他属性,(textSize,textColor之类的),只需要把 TextView替换成 com.futurestudio.foody.views.EatFoodyTextView,这个前面的是全部的包名,然后就会自己应用字体。
但是使用中会发现,虽然一些TextView的属性比如 textSize 能正常的显示,但是 textStyle 这个属性并不能正常的生效。

复杂一点的操作

添加每个ttf文件
首先将同一个系列的三种样式的 ttf 文件都加到 assets 中

在XML中使用textStyle属性
在前面已经讲解了自定义view的使用

<io.futurestud.tutorials.customfont.CustomFontTextView  android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:text="http://futurestud.io/blog/"android:textSize="18sp"android:textStyle="bold"/>

实现CustomFontTextView
为了能继续的使用系统的 android:textStyle 属性,需要一些步骤。
首先需要在代码拿到这个属性的信息,这只需要一行代码:

int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);

attr 这个值是来自 TextView 第二个构造函数中参数,我们可以使用这个对象的 getAttributeIntValue() 方法获取XML的属性。

先看一下上面代码中的 ANDROID_SCHEMA 这个参数,这个是一个常量,定义在XML的最顶部中(xmlns:android=”http://schemas.android.com/apk/res/android” ),第二个参数就是定义的属性名,最后一个参数是默认值,如果这个属性没有设置,那么就会选择 Typeface.NORMAL。
当我们考虑了样式之后,完善一下代码,全部的代码看起来就像下面这样

public class CustomFontTextView extends TextView {public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";public CustomFontTextView(Context context, AttributeSet attrs) {super(context, attrs);applyCustomFont(context, attrs);}public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);applyCustomFont(context, attrs);}private void applyCustomFont(Context context, AttributeSet attrs) {int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);Typeface customFont = selectTypeface(context, textStyle);setTypeface(customFont);}private Typeface selectTypeface(Context context, int textStyle) {/** information about the TextView textStyle:* http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle*/switch (textStyle) {case Typeface.BOLD: // boldreturn FontCache.getTypeface("SourceSansPro-Bold.ttf", context);case Typeface.ITALIC: // italicreturn FontCache.getTypeface("SourceSansPro-Italic.ttf", context);case Typeface.BOLD_ITALIC: // bold italicreturn FontCache.getTypeface("SourceSansPro-BoldItalic.ttf", context);case Typeface.NORMAL: // regulardefault:return FontCache.getTypeface("SourceSansPro-Regular.ttf", context);}
}

这样我们的自定义字体就能使用标准的应用字体样式 textStyle。

看看成果
首先我们在XML中写一些布局,包括原生的 Roboto 字体以及不同形式的自定义字体。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:text="http://futurestud.io/blog/"android:textSize="18sp"/><io.futurestud.tutorials.customfont.CustomFontTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:text="http://futurestud.io/blog/"android:textSize="18sp"/><io.futurestud.tutorials.customfont.CustomFontTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:text="http://futurestud.io/blog/"android:textSize="18sp"android:textStyle="bold"/><io.futurestud.tutorials.customfont.CustomFontTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:text="http://futurestud.io/blog/"android:textSize="18sp"android:textStyle="italic"/></LinearLayout>

setTypeface(Typeface tf)源码

参考文章:
http://www.jianshu.com/p/38f475fc07ad
http://www.jianshu.com/p/6778d048d8c6
https://developer.android.com/reference/android/graphics/Typeface.html#createFromAsset(android.content.res.AssetManager, java.lang.String)

Android自定义字体相关推荐

  1. android 自定义字体_Android自定义字体教程

    android 自定义字体 In this tutorial, we'll explain how to set up android custom fonts in TextViews and Bu ...

  2. android 加载ttf流程分析,Android自定义字体 TextView 从SD卡加载TTF字体

    Android自定义字体非常简单,能够从assets和SD卡两处加载标准的ttf字体.要实现自定义字体,只需借助工具类Typeface即可.文末有工程源码. 如果发生了Caused by: java. ...

  3. android 字体编程,Android编程之Calligraphy:Android 自定义字体库

    Calligraphy是android 自定义字体库 添加依赖 Download from Maven Central (.jar) OR Java dependencies { compile 'u ...

  4. android单线字体,Android自定义字体

    在main文件夹下,新建assets/fonts文件,添加.otf文件 image.png 字体工具类 import android.app.Application; import android.g ...

  5. android 自定义字体 ttf,Android APP支持自定义字体

    情景:需要为整个应用替换自定义字体. Android对于文字的字体设置主要是通过以下两个对象 FontFamily.Typeface 在XML文件中设置单个字体: android:id="@ ...

  6. Android 自定义字体样式 及系统默认字体样式 的设置

    Android   能添加文字的控件都可以设置字体样式 关键字是:Typeface   在Java代码中设置或者在xml文件里面设置都可以 Android系统默认给出四种样式的字体 , 分别是 : N ...

  7. Android 自定义字体样式

    参考:http://www.2cto.com/kf/201501/367220.html http://www.xuebuyuan.com/929028.html 一.自定义字体 1.android ...

  8. android使用系统字体文件,Android 自定义字体,更换系统默认显示的字体使用自定义字体...

    序言: 1.指定控件显示指定字体 有时为了美化UI,需要在指定控件中显示特定的字体,而这个字体在Android系统中却没有,此时可将需要的字体文件存放在assets文件夹中,在为控件设置Typefac ...

  9. android的自定义字体,Android 自定义字体方案

    在应用中需要配置不同的字体,而 Android 只能在 xml 中配置系统默认提供的四种字体,需要自定义的字体都需要在 Java 代码中配置. Android 默认方案 你可以通过ID查找到View, ...

  10. Android 自定义字体,设置字体

    效果图 实现代码: 1.先下载字体文件.ttf 下载链接:http://font.chinaz.com/maobiziti.html 2.main文件夹下创建fonts文件夹,.ttf文件复制到fon ...

最新文章

  1. kuangbin专题16B(kmp模板)
  2. 丛林谜题JAVA_丛林王座图文全剧情流程攻略_全谜题解答通关流程_3DM单机
  3. 分享:根据svg节点对象类型和路径值转换坐标值
  4. 可变参数列表(va_list,va_arg,va_copy,va_start,va_end)
  5. 一张正方形图片,伴随我一年半,敢问情绪的使用方法
  6. 进程的描述与控制 操作系统第二章知识点归纳总结
  7. 讨论:多核CPU+ASIC的防火墙在以后是发展方向吗?
  8. await原理 js_深入浅出node.js异步编程 及async await原理
  9. java经纬度转地址_经纬度转地址示例代码
  10. 数据库实验一——数据库定义与操作语言实验
  11. 10 大C++ Web(HTTP)开发开源框架/库推荐
  12. MEGA-X 3D打印机教程:01_我的第一台3D打印机参数
  13. matlab j计算丰水期的值,科学计算与MATLAB语言超星2020期末考试查题公众号答案
  14. Linux capability初探
  15. 【MYSQL】【基础知识】【mysql联合主键如何 in查询】
  16. python描述对象静态特性的数据为_短期借款利息数额不大,可以直接支付,不预提,在实际支付时直接记入的账户是( )。...
  17. 投资:铁矿石研究框架
  18. 学计算机未来的规划,未来学习计划范本
  19. 【HTML5】网页实用技巧3:将方形图片设置成圆形后,添加圆形虚线边框
  20. 2022系统分析师考试---选择题计算题型

热门文章

  1. EasyUI框架04——treegrid
  2. java删除文件夹下的所有文件和文件夹
  3. XCap for Mac(数位显微镜助手)
  4. StringUtil详解
  5. 西门子wincc消息队列服务器,安装Wincc 7.0 ASIA时,消息队列装不上,提示错误-工业支持中心-西门子中国...
  6. linux rzsz 安装包,安装rzsz软件包全攻略
  7. 推荐一款windows下好用的文件夹加密、文件加密软件(含使用说明)
  8. 国图三维不动产创新实践:三维地籍图制作
  9. 宗地图绘制要求和规范_宗地图绘制的基本要求与内容.ppt
  10. 服务器缺少dll文件,遇到DLL文件缺失或者损坏怎么办 DLL文件修复教程