Change app language programmatically in Android

前端 未结 30 3125
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  Happy的楠姐
    2020-11-21 05:07

    For Android 7.0 Nougat (and lower) follow this article:

    Change Language Programatically in Android

    Old answer
    This include RTL/LTR support:

    public static void changeLocale(Context context, Locale locale) {
        Configuration conf = context.getResources().getConfiguration();
        conf.locale = locale;
        Locale.setDefault(locale);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
           conf.setLayoutDirection(conf.locale);
        }
    
        context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
    }
    

提交回复
热议问题