android - 登录后更改应用程序语言 - Android
问题描述
我的 android 项目有问题。我正在尝试从我从 REST 请求中获得的值开始设置应用程序的语言。这是我在 Controller 类中的代码:
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
UserProfile userProfile = response.body();
OMSActivity.initLanguage(userProfile);
}
这是我的 Activity 类代码:
public void changeLang(Context v, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
v.getResources().updateConfiguration(config, v.getResources().getDisplayMetrics());
}
public void initLanguage(UserProfile userProfile) {
switch(userProfile.getLanguage()) {
case "en": changeLang(this,"en"); break;
case "de": changeLang(this, "de"); break;
}
}
请求成功,响应中充满了语言字符串,但不知何故,语言并没有改变。
这是 Activity 类的 onCreate :
super.onCreate(savedInstanceState);
this.omsController = new omsController(this);
omsController.getUserProfile();
setContentView(R.layout.activity_oms);
在另一个活动中,可以通过单击按钮更改语言。令人惊讶的是,它在那里工作得很好。
解决方案
推荐阅读
- php - WooCommerce 股票的哪些挂钩已保存或更新
- c# - Azure Active Directory 与 WebForms 的集成在登录时获得无限循环
- bitbucket - 如何从选定的提交中引用 BitBucket 中的一行代码,但该行不是更改的一部分
- vb.net - VB.NET Selenium 如何获得跨度的特定值
- git - 删除 git repo 和跟踪文件
- python - 使用 QLineEdit.selectAll 后如何保持打字的能力?
- javascript - 为什么映射“parseInt”无点样式不能按预期工作?
- vba - MS Access 报告中的 Code128
- php - 无法使用 PHP 7.3 将 laravel 7 部署到 Google Cloud Platform
- angularjs - 如何将 json 从 AngularJS 客户端发送到后端