字体

字体有三个粒度

1. style

宽、粗、斜

    public static final int NORMAL = 0;public static final int BOLD = 1;public static final int ITALIC = 2;public static final int BOLD_ITALIC = 3;

2. typeface

字符构造:

  • 等宽字母:MONOSPACE(每个字母一样宽)
  • 衬线:SERIF (字母的边缘会加个横线)
  • 无衬线:sans-serif, sans serif, gothic(哥特式的), sans
    public static final Typeface DEFAULT;/*** The default BOLD typeface object. Note: this may be not actually be* bold, depending on what fonts are installed. Call getStyle() to know* for sure.*/public static final Typeface DEFAULT_BOLD;/** The NORMAL style of the default sans serif typeface. */public static final Typeface SANS_SERIF;/** The NORMAL style of the default serif typeface. */public static final Typeface SERIF;/** The NORMAL style of the default monospace typeface. */public static final Typeface MONOSPACE;

3. font family

更大的粒度,比如Roboto family。看下系统默认配置下的 font family

<familyset version="22"><!-- first font is default --><!-- vivo chenyidian modify for FONT_EXTEND begin --><family name="sans-serif"><font weight="400" style="normal">VivoFont.ttf</font></family><!-- vivo chenyidian modify for FONT_EXTEND end --><!-- vivo chenyidian modify for FONT_EXTEND remove the name ="sans-serif" begin --><family><!-- vivo zhoujie modify for FONT_EXTEND end --><font weight="100" style="normal">Roboto-Thin.ttf</font><font weight="100" style="italic">Roboto-ThinItalic.ttf</font><font weight="300" style="normal">Roboto-Light.ttf</font><font weight="300" style="italic">Roboto-LightItalic.ttf</font><font weight="400" style="normal">Roboto-Regular.ttf</font><font weight="400" style="italic">Roboto-Italic.ttf</font><font weight="500" style="normal">Roboto-Medium.ttf</font><font weight="500" style="italic">Roboto-MediumItalic.ttf</font><font weight="900" style="normal">Roboto-Black.ttf</font><font weight="900" style="italic">Roboto-BlackItalic.ttf</font><font weight="700" style="normal">Roboto-Bold.ttf</font><font weight="700" style="italic">Roboto-BoldItalic.ttf</font></family>

vivo的默认字体是VivoFont.ttf,更换字体样式时,直接 替换 VivoFont.ttf 文件

TextView设置字体

根据 上述 typeface、style、family来确定真正的TypeFace加载对象。

强制设置APP的字体,且不受系统影响

  1. 添加应用theme,设置facetype为SERIF (也就是有衬线的 字体)
  2. 在Application中,反射更改TypeFace中 public static final Typeface SERIF;
  3. 如果没有指定 family、typeface,则serif的typeface,直接读 TypeFace中的static变量 SERIF

具体更改

  1. res/styles.xml
    <style name="AppTheme" parent="Theme.Design.Light.NoActionBar"><item name="android:typeface">serif</item></style>
  1. AndroidManifest.xml
    <applicationandroid:name=".YourApplication"android:theme="@style/AppTheme"
  1. YourApplication
public void onCreate() {initFont();}public void initFont() {final Typeface tf = Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf");try {Field defaultField =  Typeface.class.getDeclaredField("SERIF");defaultField.setAccessible(true);defaultField.set(null, tf);} catch (Exception e) {e.printStackTrace();}}
  • 当然推荐,异步线程initFont,因为有I/O操作.
  • /system/fonts/Roboto-Regular.ttf 你可以替换成别的
    • 解析 /system/etc/fonts.xml找第一个的字体(基本是默认字体)
    • 或者从assets里读取,你自己的ttf
Typeface tf= Typeface.createFromAsset(getAssets(),"fonts/Tahoma.ttf");
textview .setTypeface(tf);

Roboto字体

2011年4.0发行,2014年5.0重新设计了一版

Roboto (/roʊˈbɒt.oʊ/)[1] is a neo-grotesque sans-serif typeface family developed by Google as the system font for its mobile operating system Android, and released in 2011 for Android 4.0 “Ice Cream Sandwich”.[2]

The entire font family has been licensed under the Apache license.[3] In 2014, Roboto was redesigned for Android 5.0 “Lollipop”.

相关目录

系统默认字体配置文件

  • /system/etc/fonts.xml
  • /system/etc/system_fonts.xml
  • 具体目录根据 ROM版本,我测的vivo x 20A, Android 8.1.0 的路径是 /system/etc/fonts.xml

StackOverflow 解析代码1

默认字体tts文件目录

/system/fonts

$ adb shell
PD1709:/ $ ls /system/fonts
AndroidClock.ttf              NotoSansBengali-Regular.ttf             NotoSansGurmukhi-Regular.ttf              NotoSansMalayalamUI-Bold.ttf        NotoSansSylotiNagri-Regular.ttf        NotoSerif-BoldItalic.ttf
CarroisGothicSC-Regular.ttf   NotoSansBengaliUI-Bold.ttf              NotoSansGurmukhiUI-Bold.ttf               NotoSansMalayalamUI-Regular.ttf     NotoSansSymbols-Regular-Subsetted.ttf  NotoSerif-Italic.ttf
ComingSoon.ttf                NotoSansBengaliUI-Regular.ttf           NotoSansGurmukhiUI-Regular.ttf            NotoSansMandaic-Regular.ttf         NotoSansSymbols-Regular-Subsetted2.ttf NotoSerif-Regular.ttf
CutiveMono.ttf                NotoSansBrahmi-Regular.ttf              NotoSansHanunoo-Regular.ttf               NotoSansMeeteiMayek-Regular.ttf     NotoSansSyriacEastern-Regular.ttf      Padauk.ttf
DancingScript-Bold.ttf        NotoSansBuginese-Regular.ttf            NotoSansHebrew-Bold.ttf                   NotoSansMongolian-Regular.ttf       NotoSansSyriacEstrangela-Regular.ttf   Roboto-Black.ttf
//...

其他相关

升级版本可以创建字体,但我没实验。

字体:

  1. 网页字体 不受影响
  2. 其他受影响

字体大小:

  • 网页受影响
  • 其他
    • 用dp为字号单位 不受影响
    • 用sp为字号单位 受影响

代码

  • StackOverflow 解析代码1

这个应该是老版本的,不过可以借鉴下

import android.util.Xml;import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;public String getDefaultFont() {System.out.println("getFontList(): entry");File configFilename = new File("/system/etc/system_fonts.xml");String defaultFontName = "";TTFAnalyzer analyzer = new TTFAnalyzer();try {FileInputStream fontsIn = new FileInputStream(configFilename);XmlPullParser parser = Xml.newPullParser();parser.setInput(fontsIn, null);Boolean done = false;Boolean getTheText = false;int eventType;String defaultFont = "";while (!done) {eventType = parser.next();if (eventType == parser.START_TAG && parser.getName().equalsIgnoreCase("file")) {// the text is next up -- pull itgetTheText = true;}if (eventType == parser.TEXT && getTheText == true) {// first namedefaultFont = parser.getText();System.out.println("text for file tag:" + defaultFont);done = true;}if (eventType == parser.END_DOCUMENT) {System.out.println("hit end of system_fonts.xml document");done = true;}}if (defaultFont.length() > 0) {// found the font filename, most likely in /system/fonts. Now pull out the human-readable// string from the font fileSystem.out.println("Figuring out default Font info");String fontname = analyzer.getTtfFontName("/system/fonts/" + defaultFont);if ( fontname != null ) {System.out.println("found font info: " + fontname);defaultFontName = fontname;}                }} catch (RuntimeException e) {System.err.println("Didn't create default family (most likely, non-Minikin build)");// TODO: normal in non-Minikin case, remove or make error when Minikin-only} catch (FileNotFoundException e) {System.err.println("GetDefaultFont: config file Not found");} catch (IOException e) {System.err.println("GetDefaultFont: IO exception: " + e.getMessage());} catch (XmlPullParserException e) {System.err.println("getDefaultFont: XML parse exception " + e.getMessage());}return defaultFontName;
}

Android 应用统一字体设置 typeface相关推荐

  1. Android APP的字体设置

    Android系统自带有对字体的设置,这些设置是对字体的显示方式的设置,比如加粗,倾斜,下划线,字号等,但是对于字体本身,比如设置为楷体,隶书等貌似没有.Android系统只有一种默认的,如果需要修改 ...

  2. Android TextView 自定义字体设置(华文行楷TTF)

    如何在Android中,对TextView设置自己喜欢的字体呢? 原文转自:https://blog.csdn.net/shiyangkai/article/details/70257004 本文提供 ...

  3. android之统一字体大小

    字体这种大小,我希望在一个统一的地方设置,然后在界面布局的时候,统一使用一个名称来代理. 这里可以使用dimen来搞定这个事情. 步骤1:新建一个dimens.xml文件 <?xml versi ...

  4. android app全局字体,Android app全局字体设置

    相信很多对设计追求极致的开发者们对Android系统的默认字体都会感到不满意,这个时候需要使用自定义的字体,当然可以使用系统提供的Typeface来加载自定义字体,但是,一个个TextView的设置, ...

  5. 解决Android,ios字体设置font-weight后粗细显示效果不一问题

    作者:张国钰 链接:https://www.zhihu.com/question/263533998/answer/270170059 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  6. android roboto字体下载,Android字体设置及Roboto字体使用方法

    本文实例讲述了Android字体设置及Roboto字体使用方法.分享给大家供大家参考.具体分析如下: 一.自定义字体 1.android Typeface使用TTF字体文件设置字体 我们可以在程序中放 ...

  7. Android XML文件中设置字体

    Android提供三种字体:"Sans","serif"和"monospace". 1.在Android XML文件中设置字体 可以采用an ...

  8. Android 字体设置

    Android 对中文字体支持很不好~~ 需要加入相应的字体库 (1)创建布局Layout //创建线性布局LinearLayout linearLayout=newLinearLayout(this ...

  9. 深度解析Android中字体设置

    1.在Android XML文件中设置字体 可以采用Android:typeface,例如android:typeface="monospace".在这里例子中我们在Activit ...

最新文章

  1. Watir-webdriver处理table
  2. The IP you're using to send email is not authorized
  3. 一个有趣的问题,讨论讨论
  4. 周鸿祎回顾IPO一周年:保持创业心态 看好无线
  5. 教你如何一篇博客读懂设计模式之—--工厂模式
  6. linux 终端显示白底,mac终端使用Item2无法显示颜色的解决方法
  7. 大学excel题库含答案_Excel练习题及答案
  8. .tar.bz2文件怎么解压
  9. 我是如何用百度知道做小众企业站流量推广的?
  10. 如何将计算机c盘分区,无损调节电脑C盘分区,让C盘空间不再尴尬(超实用)
  11. 内外网数据交换方式有哪些?
  12. dbus-1 not met问题
  13. 沃森计算机显能耐,微型气象预报有戏
  14. 乐视x820android最新版本,乐视 Max2 Android 10更新教程
  15. sed编辑器之修改行
  16. CNN数据集——自己建立数据集要点
  17. 【判断题】【简答题】【数据库原理】
  18. 计算机跨考应用经济学,计算机专业跨考人大经济学复习经验谈br /
  19. win10——自带拼音输入法开启自学习
  20. (附源码)计算机毕业设计SSM敬老院信息管理系统

热门文章

  1. Windows 和 Linux 下后台运行 Jar包
  2. 计算机专业英语读书报告,英语读书报告范文(共6篇).doc
  3. 最全的C#图片处理类ImageHelper.cs(个人保留)
  4. taro微信小程序时间组件picker的使用--省市区三级联动
  5. 注册登录系统(含MD5加密,注册、登录、推出、注销账号)
  6. 投研报告 -用DEX技术链改投注网站的项目Betswap($BSGG)
  7. SpringMVC‘s Ediary更新中
  8. 上期所API头文件一、ThostFtdcUserApiStruct.h---API结构体的定义及工作流程(源代码6.3.19版)
  9. SANGFOR深信服AC调测
  10. Jupyter启动和快捷键大全