我正在创建一个Android应用程序,其中我设置了英语和泰米尔语的语言环境,当我点击泰米尔语时,它显示泰晤士语中的字符串,并且每件事情都运行正常。我正在进行横向模式的活动即使我使用了
,语言环境从泰米尔语变为英语的活动开始 android:configChanges="locale|orientation|screenSize|keyboardHidden"
在清单中,但它没有效果。
任何人都可以告诉我如何克服这个问题。我已经将语言存储在共享偏好中。
private static MyApplication mInstance;
public static final String PREF_NAME = "CoconutMetaData";
public static final String PREF_USER_LANGUAGE_KEY = "userLanguage";
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String myLanguage = preferences.getString(PREF_USER_LANGUAGE_KEY,"en");
// Set user specific locale (Language)
Locale mLocale = new Locale(myLanguage);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
conf.setLocale(mLocale);
}
res.updateConfiguration(conf, dm);
/**
* To display overflow menu when hard menu key is pressed (for devices that have hard menu key)
*/
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
}
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
ConnectivityReceiver.connectivityReceiverListener = listener;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
答案 0 :(得分:1)
全局声明Local
和Configuration
个对象
Locale mLocale;
Configuration conf;
并从
设置Local@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
try {
conf.setLocale(mLocale);
} catch (Exception e) {
// TODO: handle exception
}
}
尝试以下代码,这是设置本地语言的好方法
public void setLocale(String lang) {
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);
}