在我的 Android 应用程序中,我想动态更改默认语言。我已经实现了这个方法:
public void changeLanguage(String lang) { //lang="it" or "en" for example
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
{
finish();
startActivity(getIntent());
} else recreate();
}
}, 1);
}
在清单中,我将这一行添加到我的 MainActivity 中:
android:configChanges="locale|orientation"
我也试过这个:
android:configChanges="locale|layoutDirection"
此解决方案效果很好,但是一旦屏幕旋转就会恢复到默认配置并恢复语言。
我该如何解决这个问题?