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
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的字體,且不受系統影響
具體更改
- 當然推薦,異步線程initFont,因為有I/O操作.
- /system/fonts/Roboto-Regular.ttf 你可以替換成別的
- 解析 /system/etc/fonts.xml找第一個的字體(基本是默認字體)
- 或者從assets里讀取,你自己的ttf
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 //...其他相關
升級版本可以創建字體,但我沒實驗。
字體:
字體大小:
- 網頁受影響
- 其他
- 用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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3D变形:平移、旋转、缩放
- 下一篇: android sina oauth2.