这是我的代码: –
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中的语言而不会导致任何错误但是当我在棒棒糖模拟器上尝试它时它不会改变语言请帮助并提前感谢:-)
解决方法:
只是忘了在国家代码中添加新的Locale(lang.tolowerCase(),“ISO-COUNTRY_CODE”)
和应用程序运行良好.
以上是内存溢出为你收集整理的Android – 使用棒棒糖上的语言环境更改语言全部内容,希望文章能够帮你解决Android – 使用棒棒糖上的语言环境更改语言所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)