【问题标题】:Change Locale programmatically with ActionBarActivity and SupportActionBar使用 ActionBarActivity 和 SupportActionBar 以编程方式更改语言环境
【发布时间】:2015-04-25 15:49:52
【问题描述】:

我有一个多语言应用程序,其 Locale 以编程方式更改。

首次创建应用程序时,它会查看特定的SharedPreferences 文件并相应地设置区域设置。

然后用户可以进入特定的Activity P 并在可用语言中进行选择(现在只有两种语言可用)。

这就是我在 P 中所做的,langString

// set locale to that chosen by the user
((MyApp) getApplication()).setMyAppLocale(lang); 

// get the Locale object
Locale locale = ((MyApp) getApplication()).getMyAppLocale();

// classic code
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

// clear the activity stack and restart the MainActivity
Intent intent = new Intent(this, MainActivity.class);
Bundle extras = new Bundle();
extras.putBoolean(MainActivity.PARAM_DO_RESTART, true);
intent.putExtras(extras);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

活动MainActivity 接收新的Intent 并执行以下操作:

@Override
public void onNewIntent(Intent anotherIntent) {
    super.onNewIntent(anotherIntent);
    setIntent(anotherIntent);
}

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if(extras != null) {
        if (extras.getBoolean(PARAM_DO_RESTART, false)) {
            intent.putExtra(PARAM_DO_RESTART, false);
            setIntent(intent);
            finish();
            startActivity(intent);
        }
    } // if extras != null

这样,MainActivity 的 onCreate 被再次调用,资源再次加载到新的Locale 中。

一切都很好,只有一件事

所有字符串都显示在正确的语言环境中,但所有活动的标题仍使用它们最初创建的语言,当应用程序启动时。他们没有改变。我的活动实际上是ActionBarActivity 的实例,标题显示在SupportActionBar 中。

问题:如何强制根据当前的Locale更改这些标题?

注意:一个简单的解决方案是为每个Activity X 调用getSupportActionBar().setTitle(getString(R.string.title_activity_X));。但是,由于有很多活动,我宁愿找到一种自动完成的方法。

注意:我使用的是 API 级别 19,支持库 v7。

【问题讨论】:

    标签: android locale android-support-library


    【解决方案1】:

    根据answer这个标题是只读的,所以你应该手动指定标题,不能自动指定。我的猜测是因为它使用了AndroidManifest.xml 中指定的资源,无法动态更改。

    【讨论】:

    • 这就是问题所在,但它是一个错误或者应该被认为是这样。标题不是只读的。有一种方法可以自动完成,我会尽快添加答案。
    • 不幸的是,我找不到任何关于此的文档,但在我看来,它不能像 manifest 中那样完成,还有其他标签,例如 Theme,不能更改,除非你打电话给setTheme()。我将此问题添加到收藏夹,如果您找到诀窍,我想阅读答案。干杯:)
    【解决方案2】:

    我在 MiroM 提供给this question on StackOverflow 的答案中找到了一种方法。将以下代码添加到您的基础 Activity 类中,只要它最适合。

    PackageManager pm = getPackageManager();
        try {
            ActivityInfo ai = pm.getActivityInfo(this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
            if (ai.labelRes != 0) {
                getSupportActionBar().setTitle(ai.labelRes);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.stacktrace();
        }
    

    我希望其他人能解决这个问题并提供更通用的方法。当您的活动有一个共同的超类时,这种方法非常有用,我就是这种情况,但一般情况下并非如此。

    【讨论】:

    • 好一个。为了使它更通用,我认为可以将此方法放在从ActionBarActivity 扩展的BaseActivity.class 中,因此从BaseActivity 扩展的所有其他活动都将具有相同的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2015-04-26
    • 2012-01-13
    • 1970-01-01
    相关资源
    最近更新 更多