字体样式未更改 android 中的数字

font styles are not Changing neither numbers in android

我被困在这两天了,希望我能找到解决办法.. 我正在尝试本地化我的应用程序(阿拉伯语和英语) 本地化机制运行良好,文字、布局方向运行良好, 然而,有两件事没有被本地化,第一个是 numbers。 数字未本地化为阿拉伯语。 第二个是 字体样式 字体样式也没有被本地化。 但是 它将字体样式和数字更改为阿拉伯语,前提是我使用我的 USB 重新安装了该应用程序并且在安装之前配置为阿拉伯语。这是我的代码

public static void changeLocale(Activity context, Bundle... bundles) {

        MedfastSharedPreferencesHelper sharakehSharedPreference = new MedfastSharedPreferencesHelper(context);
        String lang = sharakehSharedPreference.getKey("locale");
        Locale locale = null;
        if (null == lang) {
            locale = new Locale(Resources.getSystem().getConfiguration().locale.getLanguage());
            lang = locale.getDisplayLanguage();
            if (locale.getDisplayLanguage().equalsIgnoreCase("English")) {
                locale = new Locale("ar");
                lang = "ar";
            } else {
                locale = new Locale("en");
                lang = "en";
            }

        } else if (lang.equalsIgnoreCase("ar")) {
            lang = "en";
            locale = new Locale("en");

        } else if (lang.equalsIgnoreCase("en")) {
            lang = "ar";
            locale = new Locale("ar");

        }
        sharakehSharedPreference.setKey("locale", lang);
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();

        Locale.setDefault(locale);
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.locale = locale;
        resources.updateConfiguration(configuration, displayMetrics);
        Intent intent = new Intent(context, ((Activity) context).getClass());
        if (null != bundles && bundles.length > 0 && null != bundles[0])
            intent.putExtras(bundles[0]);
        context.startActivity(intent);
        context.finish();
    }

经过研究,android 不支持字体作为资源,所以我创建了 6 个 类 以自定义我的字体并检查语言环境是阿拉伯语还是英语,并且取决于在区域设置上我加载字体,不像 android

中的 drawable

嘿,正如 Basil Battikhi 所说,该字体不适用于数字,但有一些解决方案:

1:创建您自己的自定义 textView 以便能够更改您想要的内容。

2:在strings.xml

中定义
<string name="number">%1d</string>

并像这样为您的 textView 设置数字:

textCoin.text = getString(R.string.number, it)

3:您可以使用此代码为数字设置 textView 的格式:

public static String convertEngNumToFa(String number) {
    if (AppConstants.FA_LOCALE == DataManager.Language.fa) {
        StringBuilder res = new StringBuilder();
        try {
            for (char _ch : number.toCharArray()) {
                if ((int) _ch > 47 && (int) _ch < 58) {
                    int x = (int) _ch + 1632 - 48;
                    char ch = (char) (x);

                    res.append(Character.toString(ch));
                } else {
                    res.append(Character.toString(_ch));
                }
            }
        } catch (Exception e) {
            return res.toString();
        }
        return res.toString();
    } else return number;
}