情景:需要为整个应用替换自定义字体。

Android对于文字的字体设置主要是通过以下两个对象

FontFamily、Typeface

在XML文件中设置单个字体:

android:id="@+id/tv_typeface"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:fontFamily="sans-serif-condensed"

android:text="@string/custom_typeface" />

代码中设置单个字体:

TextView tvTypeface = (TextView) findViewById(R.id.tv_typeface);

tvTypeface.setTypeface(Typeface.create("sans-serif-condensed",Typeface.NORMAL));

看到这儿,可能会有人有疑问,这里边设置的“sans-serif-condensed”从哪儿来的。有什么系统可以设置的字体呢?如果要自定义字体怎么设置?

首先我们先来看下系统内置的字体都有哪些?

/system/etc/fonts.xml

可以看到这个配置文件详细定义了具体的fontFamily名称及对应的字体文件,而我们设置的系统支持的字体就来源于这个文件,在不同的A你droid版本的系统内置的系统字体是不一样的

...

Roboto-Thin.ttf

Roboto-ThinItalic.ttf

Roboto-Light.ttf

Roboto-LightItalic.ttf

Roboto-Regular.ttf

Roboto-Italic.ttf

Roboto-Medium.ttf

Roboto-MediumItalic.ttf

Roboto-Black.ttf

Roboto-BlackItalic.ttf

Roboto-Bold.ttf

Roboto-BoldItalic.ttf

NotoSerif-Regular.ttf

NotoSerif-Bold.ttf

NotoSerif-Italic.ttf

NotoSerif-BoldItalic.ttf

DroidSansMono.ttf

CutiveMono.ttf

ComingSoon.ttf

...

/system/fonts

这个文件夹里边存储了系统具体的字体文件

AndroidClock.ttf

AndroidClock_Highlight.ttf

AndroidClock_Solid.ttf

AndroidEmoji.ttf

Clockopia.ttf

DroidNaskh-Regular.ttf

DroidNaskhUI-Regular.ttf

DroidSans-Bold.ttf

DroidSans.ttf

DroidSansArmenian.ttf

DroidSansEthiopic-Regular.ttf

DroidSansFallback.ttf

DroidSansGeorgian.ttf

DroidSansHebrew-Bold.ttf

DroidSansHebrew-Regular.ttf

DroidSansMono.ttf

DroidSerif-Bold.ttf

DroidSerif-BoldItalic.ttf

DroidSerif-Italic.ttf

DroidSerif-Regular.ttf

MTLmr3m.ttf

Roboto-Bold.ttf

Roboto-BoldItalic.ttf

Roboto-Italic.ttf

Roboto-Light.ttf

Roboto-LightItalic.ttf

Roboto-Regular.ttf

Roboto-Thin.ttf

...

设置自定义字体文件

新建/res/font 文件夹,添加自定义的字体文件 .ttf或者.otf,如添加typeface_bold.ttf自定义字体文件,

android:id="@+id/tv_typeface"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:fontFamily="@font/typeface_bold"

android:text="@string/custom_typeface" />

Typeface.create(ResourcesCompat.getFont(context, R.font.typeface_bold), Typeface.NORMAL);

SpannableString设置自定义字体

/**

* 自定义TypefaceSpan

*/

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {

super(family);

newType = type;

}

@Override

public void updateDrawState(TextPaint ds) {

applyCustomTypeFace(ds, newType);

}

@Override

public void updateMeasureState(TextPaint paint) {

applyCustomTypeFace(paint, newType);

}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {

paint.setTypeface(tf);

}

//设置自定义的TypefaceSpan

setSpan(new CustomTypefaceSpan(text.toString(), typeFace), start, end, flag);

如何为整个APP,全局替换字体呢?

添加自定义字体到Application的theme

casual

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/FontStyle">

android:name=".CustomTypefaceActivity"

android:theme="@style/FontStyle" />

为不同的语言设置不同的字体

资源文件下面添加自定义字体文件

/res/font/ibmplexsans_semibold.ttf

/res/font/sarabun_semibold.ttf

资源文件夹下面创建不同语言的theme 不同theme定义不同的字体引用

@font/ibmplexsans_semibold

@font/sarabun_semibold

代码创建工具类不同语言引用不同的字体

public class FontUtil {

public static Typeface createTypeFace(Context context, @FontConstant.FontTemplete String font) {

int fontResourceId = R.font.sarabun_regular;

if (LanguageManager.getInstance(context).isCurrentThai()) {

if (FontConstant.FONT_BOLD.equalsIgnoreCase(font)) {

fontResourceId = R.font.sarabun_bold;

}...

} else {

if (FontConstant.FONT_BOLD.equalsIgnoreCase(font)) {

fontResourceId = R.font.ibmplexsans_bold;

}...

}

return Typeface.create(ResourcesCompat.getFont(context, fontResourceId), Typeface.NORMAL);

}

}

还有一种根据不同语言定义不同字体的方法,但是这种在语言切换后定义的不同字体需要APP杀死重启才会生效

/res/fonts/ibmplexsans_semibold.ttf

/res/fonts-th-rTH/ibmplexsans_semibold.ttf //名称需要同默认字体一样 但是实际是泰文字体

android 自定义字体 ttf,Android APP支持自定义字体相关推荐

  1. Fira Code字体中增加思源黑体支持中文字体

    在Fira Code字体基础上增加思源黑体,合并到一个字体中,解决类似sourceinsight mono模式下无法显示中文的问题 下载链接如下(不需要付费,免费下载的): FiraCode字体中增加 ...

  2. IM界面高仿微信,android表情转ios表情,支持自定义表情,支持语音(实战界面)

    前言: 2018年底由子公司来到现在的集团公司,从互联网公司变成了企业公司.在最近一个项目里,做的辛辛苦苦,功能又被砍了.没有理由,心力交瘁!本来是打算自己做IM的,现在被砍了.我就把本地功能贡献出来 ...

  3. h5页面自定义字体_UIWebView使用app内自定义字体

    最近,做了个小需求.因为app是使用的自定义字体,所以产品想让h5的字体跟app的字体一致,减少脱离感.而一般来说,app内的h5页面,都是显示系统的默认字体.要想使用自定义字体,一般的做法是在h5里 ...

  4. Android开发如何让app联网,Android 开发:如何让App支持RTL模式

    前言 讲真,好久没写博客了,2016都过了一半了,赶紧重新捡起来.(个人感慨,和内容无关-- 所谓RTL,顾名思义也就是Right To Left,是一种阿拉伯语.波斯语等情况下从右往左的阅读方式.当 ...

  5. 通过js检测浏览器支持的字体,从而显示支持的字体,让用户选择。

    http://www.zhangxinxu.com/wordpress/2018/02/js-detect-suppot-font-family/ 本文根据张鑫旭文章. 字体函数: var dataF ...

  6. 全自定义PHP集成环境,支持自定义PHP,自定义Mysql,无限添加任何版本

    唯一能强行脱离依赖,在系统缺失必备组件或DLL受损的情况下依然能正常运行 优点http://blog.csdn.net/lccee/article/details/77619819      缺点,需 ...

  7. lable里的字体颜色_?APP界面的字体规范!

    文字设计是界面设计中最细节的部分,也是最不可忽视的基础部分.在界面设计过程中要考虑两大因素:文字辨识度和界面的易读性. 在任何一个有效的界面里,具有层次的设计可以将界面上重要的部分与次要的部分区分开来 ...

  8. android 按钮回弹效果,Android仿IOS回弹效果 支持任何控件

    本文实例为大家分享了Android仿IOS回弹效果的具体代码,供大家参考,具体内容如下 效果图: 导入依赖: dependencies { // ... compile 'me.everything: ...

  9. Flutter RichText支持自定义文字背景

    extended text 相关文章 Flutter RichText支持图片显示和自定义图片效果 Flutter RichText支持自定义文本溢出效果 Flutter RichText支持自定义文 ...

最新文章

  1. 关于CCRANDOM_0_1
  2. python实现一个抽象数据结构:栈
  3. python中plot柱状图-Matplotlib中柱状图bar使用
  4. 《研磨设计模式》chap17 策略模式(1) 简介
  5. Puppy Linux U盘 Linux
  6. Python 30年,你对它的核心特性了解多少?
  7. php $r,PHP
  8. 响应式精美列商城发卡源码
  9. 一、什么是类,一些关键字
  10. python统计excel数据总行数_python 统计excel行数据库
  11. AIDE手机编程初级教程(零基础向) 1.1 认识我的第一个应用
  12. python 删除文件到回收站 SHFileOperation
  13. 【Matlab】使用龙格库塔方法求积分
  14. 解决vs2019属性管理器里面没有Microsoft.Cpp.x64.user
  15. matlab中counter怎么用,matlab中fspecial函数的用法
  16. [渝粤教育] 山东职业学院 话说铁道 参考 资料
  17. html播放ppt插件,lightslider-支持移动触摸的轻量级jQuery幻灯片插件
  18. ubuntu安装pinta(图片编辑器)
  19. 计算机无法屏幕亮度,win7电脑屏幕亮度无法调节怎么办?调节屏幕亮度的方法...
  20. 英文歌曲:What I Have Done(变形金刚第一部主题曲)

热门文章

  1. windows安全事件查看及安全事件id汇总
  2. PLSQL - 递归子查询RSF打破CONNECT BY LOOP限制
  3. Linux Bridge的IP NAT细节探析-填补又一坑的过程
  4. 大数据比较 同比与环比的区别
  5. 【游戏】LOL只能攻击英雄,点不了小兵解决办法
  6. 简单的哈夫曼树程序实现
  7. matlab+whisker,Matlab Boxplot:使用特定的百分位数作为上部胡须或在手动上部胡须编辑后删除多余的异常值...
  8. 京东云修改Hostname,Centos7修改Hostname
  9. 我被List中remove()方法的陷阱,坑惨了!
  10. 输入方向的流量控制 --ifb