这是我的代码: -
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);`langData = "hi"`
setLocale(langData);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//setLocale(langData);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, 3000);
}
public void setLocale(String lang) {
Resources res = this.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(conf, new Locale(lang.toLowerCase()));
} else {
setSystemLocaleLegacy(conf, new Locale(lang.toLowerCase()));
}
res.updateConfiguration(conf, dm);
//startActivity(new Intent(this,MainActivity.class));
//finish();
//this.setContentView(R.layout.activity_main);
}
@SuppressWarnings("deprecation")
public Locale getSystemLocaleLegacy(Configuration config) {
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public Locale getSystemLocale(Configuration config) {
return config.getLocales().get(0);
}
@SuppressWarnings("deprecation")
public void setSystemLocaleLegacy(Configuration config, Locale locale) {
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public void setSystemLocale(Configuration config, Locale locale) {
config.setLocale(locale);
}
}
当我在我的模拟器上运行此代码时 (Android 7.0 Api 24)它可以平滑地更改我的MainActivity中的语言,而不会导致任何错误,但是当我在棒棒糖模拟器上尝试它时,它确实没有更改语言请提前帮助并提前感谢: - )
答案 0 :(得分:5)
忘了在那里添加国家/地区代码new Locale(lang.toLowerCase(),"ISO- COUNTRY_CODE")
和应用程序运行良好。