当我更改语言环境并在android中应用整个应用程序的Font时,限制用英语更改文本

时间:2015-10-23 08:54:45

标签: android-fonts

我正在尝试为我的整个应用程序应用字体,该字体支持多种印度语言。我正在将下面给出的代码应用于棒棒糖上或下方。

public void setDefaultAppFont(String fontAssetName)
{
    if (Build.VERSION.SDK_INT >= 21) 
    {
        Map newMap = new HashMap();
        final String typefaceFieldName = "sans-serif";
        newMap.put(typefaceFieldName, Typeface.createFromAsset(appInstance.getAssets(), fontAssetName));
        try 
        {
            final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
            staticField.setAccessible(true);
            staticField.set(null, newMap);
        } 
        catch (NoSuchFieldException e) 
        {
            e.printStackTrace();
        } 
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
    else
    {
        Typeface typeface = Typeface.createFromAsset(appInstance.getAssets(), fontAssetName);
        try 
        {
            Field staticField = Typeface.class.getDeclaredField("DEFAULT");
            staticField.setAccessible(true);
            staticField.set(null, typeface);

            staticField = Typeface.class.getDeclaredField("DEFAULT_BOLD");
            staticField.setAccessible(true);
            staticField.set(null, typeface);

            staticField = Typeface.class.getDeclaredField("SANS_SERIF");
            staticField.setAccessible(true);
            staticField.set(null, typeface);

            staticField = Typeface.class.getDeclaredField("SERIF");
            staticField.setAccessible(true);
            staticField.set(null, typeface);

            staticField = Typeface.class.getDeclaredField("MONOSPACE");
            staticField.setAccessible(true);
            staticField.set(null, typeface);

        } 
        catch (NoSuchFieldException e) 
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) 
        {
            e.printStackTrace();
        }
    }
}

以上代码允许我们更改lollipop设备上或下方的默认英文字体,但是它无法呈现来自字符串文件的默认英文字母,并在非英语时自动将这些英文字母转换为非英语字母由用户选择。 例如 : 我在我的应用程序中使用了一个微调器,它有十种语言选项,如英语(用英文字母书写),Kannada(写的是kannada字母),Marathi(用马拉地语字母书写),古吉拉特语(用古吉拉特语字母书写)等等。用户从该微调器中选择一个特定选项,整个应用程序语言和字体更改。它还改变了旋转器中的英语(用英文字母书写),即使用户选择任何语言选项,也不允许在其他语言中进行更改。

0 个答案:

没有答案