android - 如何获取 TextView 上应用的字体名称

标签 android android-layout android-fonts

Textview label = (TextView) findViewById(R.id.item_title);
label.setText("Solve My Issue !");
Log.d("TAG","Font-Family : "+ String.valueOf(label.getTypeface()));

当我看到它返回的日志时 Font-Family : android.graphics.Typeface@7f37f870

如何知道字体家族的名称?可能吗?

最佳答案

getTypeface()方法返回 label 的字体,而此 Tyepface 实例的值为 Map<String, Typeface> sSystemFontMap , 这是 Typeface 的静态字段.所以现在你得到了值(value),通过反射你可以得到 map sSystemFontMap ,然后想找到key,也就是字体的名字。

protected Map<String, Typeface> getSSystemFontMap() {
    Map<String, Typeface> sSystemFontMap = null;
    try {
        //Typeface typeface = Typeface.class.newInstance();
        Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
        Field f = Typeface.class.getDeclaredField("sSystemFontMap");
        f.setAccessible(true);
        sSystemFontMap = (Map<String, Typeface>) f.get(typeface);
        for (Map.Entry<String, Typeface> entry : sSystemFontMap.entrySet()) {
            Log.d("FontMap", entry.getKey() + " ---> " + entry.getValue() + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sSystemFontMap;
}

private static List<String> getKeyWithValue(Map map, Typeface value) {
    Set set = map.entrySet();
    List<String> arr = new ArrayList<>();
    for (Object obj : set) {
        Map.Entry entry = (Map.Entry) obj;
        if (entry.getValue().equals(value)) {
            String str = (String) entry.getKey();
            arr.add(str);
        }
    }
    return arr;
}

我测试了一下,列表arr包含以下字符串

sans-serif
tahoma
arial
helvetica
verdana

这并不奇怪,因为Android 系统使用与上述五个名称相同的字体。 (可能各个系统版本有些差异,更多信息请前往 /system/etc/fonts.xml )

<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" />

由此可见。 sans-serif,tahoma,arial,helvetica,verdana是一样的东西。字体系列的不同名称sans-serif

关于android - 如何获取 TextView 上应用的字体名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37696436/

相关文章:

android - 如何从 RecyclerView 中删除 SnapHelper

android - 大屏幕(2560x1600、2048x1536、1920x1200)宽度 dp

java - 我该怎么做才能使我的 android Activity 设计更像这个应用程序屏幕截图图像?

android - 如何在 Android 中对齐 GridView 顶部的项目?

android - TextView 中的乌尔都语字体

android - CollapsingToolbarLayout字体未通过编程方式更改

android - 如何在 Github 上创建库并通过 Android Studio 中的 gradle 依赖项使用它

android - 统计整数数组值Android

android - PagerSlidingTabStrip 改变标题的字体

android - 如何设置多个SurfaceView的Z顺序