Android 应用统一字体设置 typeface

字体

字体有三个粒度

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
    <application
		 android: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 it
                getTheText = true;
            }
            if (eventType == parser.TEXT && getTheText == true) {
                // first name
                defaultFont = 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 file
            System.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; 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值