我想在更改应用程序区域设置时更改字符串
存在配置文件片段
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.changeLanguageButton:
if (PrefManager.getLocale(getActivity()).equals("en")) {
setLocale("ar");
} else {
setLocale("en");
}
break;
}
}
public void setLocale(String lang ) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(getActivity(), lang);
Intent intent = new Intent(getActivity(), SplashActivity.class);
startActivity(intent);
}
有飞溅活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (PrefManager.getLocale(this) == null){
setLocale("en");
}else if (PrefManager.getLocale(this).equals("en")){
setLocale("en");
}else {
setLocale("ar");
}
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, 3000);
}
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
this.getBaseContext().getResources().updateConfiguration(config, this.getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(this, lang);
}
问题是这个代码在API 23的设备中工作并翻译所有字符串,但在API 22的设备中不工作它只改变布局方向
我的minSDKVersion 19&targetSDK版本29
如果有人对感兴趣,我找到了答案
我将阿拉伯语字符串文件名从"改为";字符串(ar-rEG(";至";字符串(ar(";
而且运行良好