Html.fromHtml函数中的“font”标签支持哪些字体?

5

Html.fromHtml 几乎没有关于它支持哪些标签的文档。

有很多网站(如这里)写道它支持 "font" 标签,但我无法确定在设置此标签中的 "face" 属性时可用的字体。

我可以假设哪些字体总是可用的?它们中有哪些仅来自特定的 Android 版本?有一个列表吗?

编辑:答案似乎应该在 "/system/etc/fonts.xml" 文件中。以下是 Android 6.0.1 上的内容:

<familyset version="22">
    <!-- first font is default -->
    <family name="sans-serif">
        <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>

    <!-- Note that aliases must come after the fonts they reference. -->
    <alias name="sans-serif-thin" to="sans-serif" weight="100" />
    <alias name="sans-serif-light" to="sans-serif" weight="300" />
    <alias name="sans-serif-medium" to="sans-serif" weight="500" />
    <alias name="sans-serif-black" to="sans-serif" weight="900" />
    <alias name="arial" to="sans-serif" />
    <alias name="helvetica" to="sans-serif" />
    <alias name="tahoma" to="sans-serif" />
    <alias name="verdana" to="sans-serif" />

    <family name="sans-serif-condensed">
        <font weight="300" style="normal">RobotoCondensed-Light.ttf</font>
        <font weight="300" style="italic">RobotoCondensed-LightItalic.ttf</font>
        <font weight="400" style="normal">RobotoCondensed-Regular.ttf</font>
        <font weight="400" style="italic">RobotoCondensed-Italic.ttf</font>
        <font weight="700" style="normal">RobotoCondensed-Bold.ttf</font>
        <font weight="700" style="italic">RobotoCondensed-BoldItalic.ttf</font>
    </family>
    <alias name="sans-serif-condensed-light" to="sans-serif-condensed" weight="300" />

    <family name="serif">
        <font weight="400" style="normal">NotoSerif-Regular.ttf</font>
        <font weight="700" style="normal">NotoSerif-Bold.ttf</font>
        <font weight="400" style="italic">NotoSerif-Italic.ttf</font>
        <font weight="700" style="italic">NotoSerif-BoldItalic.ttf</font>
    </family>
    <alias name="times" to="serif" />
    <alias name="times new roman" to="serif" />
    <alias name="palatino" to="serif" />
    <alias name="georgia" to="serif" />
    <alias name="baskerville" to="serif" />
    <alias name="goudy" to="serif" />
    <alias name="fantasy" to="serif" />
    <alias name="ITC Stone Serif" to="serif" />

    <family name="monospace">
        <font weight="400" style="normal">DroidSansMono.ttf</font>
    </family>
    <alias name="sans-serif-monospace" to="monospace" />
    <alias name="monaco" to="monospace" />

    <family name="serif-monospace">
        <font weight="400" style="normal">CutiveMono.ttf</font>
    </family>
    <alias name="courier" to="serif-monospace" />
    <alias name="courier new" to="serif-monospace" />

    <family name="casual">
        <font weight="400" style="normal">ComingSoon.ttf</font>
    </family>

    <family name="cursive">
        <font weight="400" style="normal">DancingScript-Regular.ttf</font>
        <font weight="700" style="normal">DancingScript-Bold.ttf</font>
    </family>

    <family name="sans-serif-smallcaps">
        <font weight="400" style="normal">CarroisGothicSC-Regular.ttf</font>
    </family>

    <!-- fallback fonts -->
    <family variant="elegant">
        <font weight="400" style="normal">NotoNaskhArabic-Regular.ttf</font>
        <font weight="700" style="normal">NotoNaskhArabic-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoNaskhArabicUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoNaskhArabicUI-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansEthiopic-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansEthiopic-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansHebrew-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansHebrew-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansThai-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansThai-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansThaiUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansThaiUI-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansArmenian-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansArmenian-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansGeorgian-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansGeorgian-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansDevanagari-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansDevanagari-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansDevanagariUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansDevanagariUI-Bold.ttf</font>
    </family>
    <!-- Gujarati should come after Devanagari -->
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansGujarati-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansGujarati-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansGujaratiUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansGujaratiUI-Bold.ttf</font>
    </family>
    <!-- Gurmukhi should come after Devanagari -->
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansGurmukhi-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansGurmukhi-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansGurmukhiUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansGurmukhiUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansTamil-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansTamil-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansTamilUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansTamilUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansMalayalam-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansMalayalam-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansMalayalamUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansMalayalamUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansBengali-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansBengali-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansBengaliUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansBengaliUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansTelugu-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansTelugu-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansTeluguUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansTeluguUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansKannada-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansKannada-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansKannadaUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansKannadaUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansOriya-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansOriya-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansOriyaUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansOriyaUI-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSinhala-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansSinhala-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansKhmer-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansKhmer-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansKhmerUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansKhmerUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansLao-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansLao-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansLaoUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansLaoUI-Bold.ttf</font>
    </family>
    <family variant="elegant">
        <font weight="400" style="normal">NotoSansMyanmar-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansMyanmar-Bold.ttf</font>
    </family>
    <family variant="compact">
        <font weight="400" style="normal">NotoSansMyanmarUI-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansMyanmarUI-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansThaana-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansThaana-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansCham-Regular.ttf</font>
        <font weight="700" style="normal">NotoSansCham-Bold.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansBalinese-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansBamum-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansBatak-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansBuginese-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansBuhid-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansCanadianAboriginal-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansCherokee-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansCoptic-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansGlagolitic-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansHanunoo-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansJavanese-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansKayahLi-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansLepcha-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansLimbu-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansLisu-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansMandaic-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansMeeteiMayek-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansNewTaiLue-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansNKo-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansOlChiki-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansRejang-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSaurashtra-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSundanese-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSylotiNagri-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSyriacEstrangela-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansTagbanwa-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansTaiTham-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansTaiViet-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansTibetan-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansTifinagh-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansVai-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansYi-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansSymbols-Regular-Subsetted.ttf</font>
    </family>
    <family lang="zh-Hans">
        <font weight="400" style="normal">NotoSansSC-Regular.otf</font>
    </family>
    <family lang="zh-Hant">
        <font weight="400" style="normal">NotoSansTC-Regular.otf</font>
    </family>
    <family lang="ja">
        <font weight="400" style="normal">NotoSansJP-Regular.otf</font>
    </family>
    <family lang="ko">
        <font weight="400" style="normal">NotoSansKR-Regular.otf</font>
    </family>
    <family>
        <font weight="400" style="normal">NanumGothic.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoColorEmoji.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">DroidSansFallback.ttf</font>
    </family>
    <family lang="ja">
        <font weight="400" style="normal">MTLmr3m.ttf</font>
    </family>
    <!--
        Tai Le and Mongolian are intentionally kept last, to make sure they don't override
        the East Asian punctuation for Chinese.
    -->
    <family>
        <font weight="400" style="normal">NotoSansTaiLe-Regular.ttf</font>
    </family>
    <family>
        <font weight="400" style="normal">NotoSansMongolian-Regular.ttf</font>
    </family>
</familyset>

我需要指出fromHtml()不是一个命令,而是一个方法 - Marcin Orlowski
@CommonsWare 好的,我打开了它,那里有很多标签。 我应该使用“family”标签还是“alias”标签? 什么是“variant”? 我已经更新了问题以包含XML文件内容。 - android developer
你需要在“Typeface”代码中搜索。 - CommonsWare
@pskink 再次强调,我不想使用任意字体。我想知道哪些字体可以使用我所描述的标签来直接调用。 - android developer
1
很不幸的是,Html.TagHandler 没有传递标签属性,所以你需要类似于 这样 的东西。 - pskink
显示剩余15条评论
1个回答

4
使用"/system/etc/fonts.xml"文件,我创建了以下代码:
    textView.setText(Html.fromHtml("Normal <br/>" +
            "<font face='sans-serif'>sans-serif</font> <br/>" +
            "<font face='sans-serif-thin'>sans-serif-thin</font> <br/>" +
            "<font face='sans-serif-light'>sans-serif-light</font> <br/>" +
            "<font face='sans-serif-medium'>sans-serif-medium</font> <br/>" +
            "<font face='sans-serif-black'>sans-serif-black</font> <br/>" +
            "<font face='sans-serif-condensed'>sans-serif-condensed</font> <br/>" +
            "<font face='sans-serif-condensed-light'>sans-serif-condensed-light</font> <br/>" +
            "<font face='serif'>serif</font> <br/>" +
            "<font face='monospace'>monospace</font> <br/>" +
            "<font face='serif-monospace'>serif-monospace</font> <br/>" +
            "<font face='casual'>casual</font> <br/>" +
            "<font face='cursive'>cursive</font> <br/>" +
            "<font face='sans-serif-smallcaps'>sans-serif-smallcaps</font> <br/>" +
            "<font face='elegant'>elegant</font> <br/>" +
            "<font face='compact'>compact</font> <br/>"
    ));

这是结果:

enter image description here

看起来所有这些字体都被支持了,除了“elegant”和“compact”,它们在那里似乎不是真正的字体(所以也许我没有理解它们的目的)。
此外,这是在Android 6.0.1上测试的。我想知道它在旧版本上的表现如何,在Android N中是否有新版本。
在Android 4.0模拟器上运行此代码的结果如下:

enter image description here

看起来它没有大多数字体。
以下是在Android 4.1.2上使用Xperia J看到的内容:

enter image description here

所以在这里似乎有更多的字体可用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接