日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 应用统一字体设置 typeface

發布時間:2023/12/20 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的字體,且不受系統影響

  • 添加應用theme,設置facetype為SERIF (也就是有襯線的 字體)
  • 在Application中,反射更改TypeFace中 public static final Typeface SERIF;
  • 如果沒有指定 family、typeface,則serif的typeface,直接讀 TypeFace中的static變量 SERIF
  • 具體更改

  • res/styles.xml
  • <style name="AppTheme" parent="Theme.Design.Light.NoActionBar"><item name="android:typeface">serif</item></style>
  • AndroidManifest.xml
  • <applicationandroid:name=".YourApplication"android:theme="@style/AppTheme"
  • 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 //...

    其他相關

    升級版本可以創建字體,但我沒實驗。

    字體:

  • 網頁字體 不受影響
  • 其他受影響
  • 字體大小:

    • 網頁受影響
    • 其他
      • 用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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。