用户select语言时如何更改应用程序的语言?

我希望我的应用支持三种语言西class牙语,葡萄牙语和英语。 并给出select应用程序中的语言select

1)3个可绘制文件夹drawable-es,drawable-pt,drawable。

2)3个值文件夹值-es,values-pt,values.Change根据语言的String.xml值。

我有imageViewselectlanguage.When点击菜单打开包含选项英语,西class牙语,葡萄牙语。

我通过此代码在选项select中设置了区域设置

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.en: Locale locale = new Locale("en"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); Toast.makeText(this, "Locale in English !", Toast.LENGTH_LONG).show(); break; case R.id.pt: Locale locale2 = new Locale("pt"); Locale.setDefault(locale2); Configuration config2 = new Configuration(); config2.locale = locale2; getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics()); Toast.makeText(this, "Locale in Portugal !", Toast.LENGTH_LONG).show(); break; case R.id.es: Locale locale3 = new Locale("es"); Locale.setDefault(locale3); Configuration config3 = new Configuration(); config3.locale = locale3; getBaseContext().getResources().updateConfiguration(config3, getBaseContext().getResources().getDisplayMetrics()); Toast.makeText(this, "Locale in Spain !", Toast.LENGTH_LONG).show(); break; } return super.onOptionsItemSelected(item); } 

我已经声明在清单android:configChanges =“locale”

它的工作,但它有一些问题。

问题:-

1)select语言时,语言select的图像的画面不变,其他画面变更。

2)方向改变后,根据手机的区域设置恢复语言。

这是摘录的网页: http : //android.programmerguru.com/android-localization-at-runtime/

当用户从语言列表中select它时,更改应用程序的语言非常简单。 有一个像下面这样的接受区域设置为String的方法(比如'en'英文,'hi'代表印地语),为你的Appconfiguration语言环境并刷新你当前的活动以反映语言的变化。 您应用的语言环境将不会更改,直到您再次手动更改为止。

 public void setLocale(String lang) { Locale myLocale = new Locale(lang); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); Intent refresh = new Intent(this, AndroidLocalize.class); startActivity(refresh); finish(); } 

确保你导入了下面的包:

 import java.util.Locale; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.util.DisplayMetrics; 

添加到清单活动android:configChanges =“locale | orientation”

您应该从清单中删除android:configChanges="locale" ,这会导致活动重新加载,或者覆盖onConfigurationChanged方法:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // your code here, you can use newConfig.locale if you need to check the language // or just re-set all the labels to desired string resource } 

Udhay的示例代码运行良好。 除了Sofiane Hassaini和Chirag SolankI的问题外,再入场是行不通的。 我尝试调用Udhay的代码,而无需重新启动onCreate(),super.onCreate(savedInstanceState);之前的活动。 那就OK了! 只有一点问题,菜单string仍然不会更改为设置的Locale。

  public void setLocale(String lang) { //call this in onCreate() Locale myLocale = new Locale(lang); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); //Intent refresh = new Intent(this, AndroidLocalize.class); //startActivity(refresh); //finish(); } 

好的解决scheme在这里解释得很好 但是这里还有一个。

创build您自己的CustomContextWrapper类,扩展ContextWrapper并使用它来更改完整应用程序的区域设置。 这是一个使用GIST 。

然后在活动生命周期方法attachBaseContext使用保存的区域标识符(例如'hi'来调用CustomContextWrapper以获得印地语语言。 用法在这里:

 @Override protected void attachBaseContext(Context newBase) { //fetch from shared preference also save to the same when applying. default here is en = English String language = MyPreferenceUtil.getInstance().getString("saved_locale", "en"); super.attachBaseContext(SnapContextWrapper.wrap(newBase, language)); }