首页 > 解决方案 > 登录后更改应用程序语言 - 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);

在另一个活动中,可以通过单击按钮更改语言。令人惊讶的是,它在那里工作得很好。

标签: androidrest

解决方案


推荐阅读