Change app language programmatically in Android

前端 未结 30 3727
面向向阳花
面向向阳花 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条回答
  •  长情又很酷
    2020-11-21 05:08

    At first create multi string.xml for different languages; then use this block of code in onCreate() method:

    super.onCreate(savedInstanceState);
    String languageToLoad  = "fr"; // change your language here
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
    

提交回复
热议问题