如何获取已安装的Android系统字体列表并将其应用于自定义键盘

时间:2019-03-07 04:25:14

标签: android android-softkeyboard android-fonts

我必须制作一个可以检索预装Android操作系统的字体列表的应用程序。我需要显示系统字体,并且可以选择为我的应用程序设置该字体。

我的应用程序中有许多功能,我必须获取随机的系统字体并将此字体样式应用于我的自定义软键盘。如何将这种字体应用于我的自定义软键盘。

如何访问此字体并将其应用于我的应用程序?

3 个答案:

答案 0 :(得分:1)

文件管理器类以加载系统字体

public class FontManager {
    // This function enumerates all fonts on Android system and returns the HashMap with the font
    // absolute file name as key, and the font literal name (embedded into the font) as value.
    static public HashMap< String, String > enumerateFonts()
    {
        String[] fontdirs = { "/system/fonts", "/system/font", "/data/fonts" };
        HashMap< String, String > fonts = new HashMap< String, String >();
        TTFAnalyzer analyzer = new TTFAnalyzer();

        for ( String fontdir : fontdirs )
        {
            File dir = new File( fontdir );

            if ( !dir.exists() )
                continue;

            File[] files = dir.listFiles();

            if ( files == null )
                continue;

            for ( File file : files )
            {
                String fontname = analyzer.getTtfFontName( file.getAbsolutePath() );


                if ( fontname != null ) {
//                    Log.d("fonts", fontname+" : "+file.getAbsolutePath());
                    fonts.put(file.getAbsolutePath(), fontname);
                }

            }




        }

        return fonts.isEmpty() ? null : fonts;
    }

字体详细信息模型类:

public class FontDetail
{
    String sFontNames;
    String sFontPaths;
    String sFontType;

    public FontDetail(String sFontNames, String sFontPaths, String sFontType)
    {
        this.sFontNames = sFontNames;
        this.sFontPaths = sFontPaths;
        this.sFontType = sFontType;
    }

    public String getsFontNames()
    {
        return sFontNames;
    }

    public void setsFontNames(String sFontNames)
    {
        this.sFontNames = sFontNames;
    }

    public String getsFontPaths()
    {
        return sFontPaths;
    }

    public void setsFontPaths(String sFontPaths)
    {
        this.sFontPaths = sFontPaths;
    }

    public String getsFontType()
    {
        return sFontType;
    }

    public void setsFontType(String sFontType) {
        this.sFontType = sFontType;
    }
}

保存字体列表的数组列表:

   ArrayList<FontDetail> arrLstFontDetail = new ArrayList<FontDetail>();

在arrLstFontDetails中加载系统字体:

public void loadSystemFontsToListView() {
      try {
         Iterator<?> iter = FontManager.enumerateFonts().entrySet().iterator();

         while (iter.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry mEntry = (Map.Entry) iter.next();
            LogMaintain.ShowLog(LogMaintain.LogType.Error, "System : " + (String) mEntry.getValue() + "Key: " + (String) mEntry.getKey());
            arrLstFontDetail.add(new FontDetail((String) mEntry.getValue(), (String) mEntry.getKey(), "System"));
         }
}

答案 1 :(得分:0)

Typeface是可以帮助您获取所有已安装字体的列表的类。然后,您可以映射字体。该链接可以帮助您: How to get name of the font applied on the textview

答案 2 :(得分:0)

How to retrieve a list of available/installed fonts in android?

从最初的问题上面我得到了我的答案,但我正在用我真正想做的事情扩展我的答案。我们如何在应用程序中访问这些字体。

我们可以使用此功能(Typeface.createFromFile("your file")

从路径访问系统
      String path = "/system/fonts";
      File file = new File(path);
      File ff[] = file.listFiles();

       for(int i = 0 ; i < ff.length ; i++){
       fontName.setText(file.getName());
       fontName.setTypeface(Typeface.createFromFile(ff[i]));   
       }