2010-10-25 112 views
3

我已阅读过有关本地化的信息:http://developer.android.com/guide/topics/resources/localization.html 但我需要在运行时切换我的android应用程序中的语言,例如,通过微调。如何在运行时更改语言

我试图做SUBJ这样

DisplayMetrics dm = res.getDisplayMetrics(); 
Configuration conf = res.getConfiguration(); 
conf.locale = new Locale(language_code.toLowerCase(), 
coutry_code.toUpperCase()); 
res.updateConfiguration(conf, dm); 

但所有更改只有在重新启动应用程序 谁能帮我申请后?

回答

0

那么在本地化指南中,它会自动更新......当我在应用程序中使用spinners时,它会正确更新..您在类的底部设置了spinner类吗?或只是在你的微调的选择,让它重新启动你的意图,如:

//spinner class start... 
if(selected.equals("english") //given selected is a string returned by Spinner 
{ 
    //normal spinner content you have goes here, then 
    //for example, finish method, then restart with an intent 
    finish() 
    Intent myIntent = new Intent(main.this,main.class); 
    main.this.startActivity(myIntent); 
} 
//or.. 
if(selected.equals("french"){ // continued.. 
    refresh(); 
} 

//given that refresh(); 
public void refresh(){ 
    finish() 
    Intent myIntent = new Intent(main.this,main.class); 
    main.this.startActivity(myIntent); 
} 
+0

谢谢,山姆! 也许你可以回答如何从某些地区获取资源字符串? – 2010-10-28 17:00:34

+0

我很抱歉,你将不得不再次唠叨..我不确定你在问什么。你是否问过,如何有多个地方获取字符串? – Samuel 2010-11-02 17:01:29

+0

我有几个语言环境 - en,ru,fr,de。当在选定的设备设置中时法语区域设置和我试图调用getString(strId)返回给我的值将来自fr/strings.xml,但如果我想从另一个字符串获取字符串,例如英语?是否存在某种获得像getString(strId,locale)这样的字符串的方法? – 2010-11-08 16:18:05