的Andr​​oid如何改变应用程序的语言在运行时 [英] Android how to change the application language at runtime

查看:113
本文介绍了的Andr​​oid如何改变应用程序的语言在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让用户修改应用中使用微调的语言(或任何方式)。 我尝试过很多方法,但他们改变这种活动的语言不是所有的活动,我要救它,以便当用户重新启动应用程序,他会发现最后选用的语言。

I want to let the user change the language of my application using spinner (or any way). I tried many ways but they change the language of this activity not all activities, and I want to save it so when the user restart the app he will find the last choosed language.

推荐答案

您可以使用此code的微调或任何你想要的

you can use this code in spinner or any way you want

String languageToLoad  = "en"; // your language 
Locale locale = new Locale(languageToLoad);  
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config,  
getBaseContext().getResources().getDisplayMetrics());

那么你应该保存语言像这样

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();  

和从使用相同的code在每一个活动的onCreate()加载 languageToLoad 共享preferences

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences

这篇关于的Andr​​oid如何改变应用程序的语言在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆