如何在运行时更改语言

3
我曾在这里阅读有关本地化的内容:http://developer.android.com/guide/topics/resources/localization.html。但我需要在运行时通过Spinner等方式切换Android应用程序的语言。
我尝试以以下方式进行操作:
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase(),
coutry_code.toUpperCase());
res.updateConfiguration(conf, dm);

但是所有的更改都只有在重新启动应用程序后才会生效。有人能帮忙吗?

1个回答

0

在本地化指南中,它会自动更新... 当我在应用程序中使用 spinner 时,它会正确更新... 你的类底部是否设置了 spinner 类?或者在 spinner 的选项中,像这样重新启动你的 intent:

//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);
}

谢谢,Sam! 也许你能回答如何从特定的语言环境中获取资源字符串? - John Wein
抱歉,您需要再详细解释一下。我不确定您的问题是什么。您是在询问如何从多个位置获取字符串吗? - Samuel
我有几种本地化设置 - 英语、俄语、法语、德语。当设备设置中选择了法语本地化并尝试调用 getString(strId) 时,返回给我的值将来自 fr/strings.xml,但如果我想从另外一种本地化设置中获取字符串,例如英语,是否存在某种方式可以像 getString(strId, locale) 这样获取字符串呢? - John Wein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接