地点:onConfigurationChanged不叫 [英] Locale: onConfigurationChanged not called

查看:155
本文介绍了地点:onConfigurationChanged不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的语言来动态的改变,我尝试使用onConfigurationChanged但它不会被调用。我有一个创建我的行动吧和viewpager一个MainActivity。我的网页其余的都是碎片。在我的SettingsFragment我有一个按钮来切换语言为法语。

I want my language to change dynamically and I am trying to use onConfigurationChanged but it is not being called. I have a MainActivity that creates my action bar and viewpager. The rest of my pages are Fragments. In my SettingsFragment I have a button to switch the language to French.

langChange.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View vi) {
            MainActivity main = (MainActivity)getActivity();
            main.langChange();
        }

});

然后在我的MainActivity我有

Then in my MainActivity I have

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if (locale != null){
        newConfig.locale = locale;
        Locale.setDefault(locale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    }
}

public void langChange(){
    if(currentLanguage == FRENCH_LANGUAGE){
        locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration c = getBaseContext().getResources().getConfiguration();
        c.locale = locale;
        getBaseContext().getResources().updateConfiguration(c,getBaseContext().getResources().getDisplayMetrics());
        currentLanguage = "English";
    }
    else if(currentLanguage == ENGLISH_LANGUAGE){
        locale = new Locale("fr");
        Locale.setDefault(locale);
        Configuration c = getBaseContext().getResources().getConfiguration();
        c.locale = locale;
        getBaseContext().getResources().updateConfiguration(c,getBaseContext().getResources().getDisplayMetrics());
        currentLanguage = "French";
    }
    actionBar.setSelectedNavigationItem(actionBar.getTabCount() - 1); //This just puts it back to the settings tab
}

onConfigurationChanged 不会被调用。在我的清单我有:

The onConfigurationChanged is not being called. In my manifest I have:

<activity android:name="MainActivity" android:screenOrientation="portrait" android:configChanges="locale"></activity>

我曾尝试加入一种或所有这些选项方向| keyboardHidden |。屏幕尺寸,但没有成功。

原因所有这一切都是因为我想改变动作条的文本和所有其他文本一旦我按一下按钮。我有一个单独的字符串文件的法语。

The reason for all of this is because I want to change the actionBar text and all the other text once I click the button. I have a separate strings file for French.

任何帮助将是巨大的。

推荐答案

好吧,我考虑过这一点。

Ok, I've looked into this a bit.

我不知道,为什么 onConfigurationChanged 方法不叫,所以我希望有人能启发我们在这个部分。

I'm not sure, why the onConfigurationChanged method isn't called, so I'm hoping someone can enlighten us on this part.

在我的搜索我在此教程,这实际上改变区域设置,通过更改配置。

In my search I stumbled upon this tutorial , which actually change the Locale, by changing the configuration.

您code看起来很像这个教程实际; - )

Your code looks a lot like this tutorial actually ;-)

不管怎么说,关于教程和code,重要的是这种方法:

Anyways, the important thing about the tutorial and the code is this method:

private void updateTexts() {
    txt_hello.setText(R.string.hello_world);
    btn_en.setText(R.string.btn_en);
    btn_ru.setText(R.string.btn_ru);
    btn_fr.setText(R.string.btn_fr);
    btn_de.setText(R.string.btn_de);
}

这就是神奇发生;你改变你的语言环境后,你将需要重新装入您的资源,因为你告诉Android的要处理你自己,你需要通过设置UI项​​具体重装所有文本,某些配置再次文本。

This is where the "magic" happens; after you've changed your locale you will need to reload your resources and since you told Android you want to handle some configurations on your own you need to specifically reload all your text, by setting your UI items texts again.

在这样做,应用程序将在特定区域设置文件夹中加载字符串。

When this is done, the app will load the strings from the specific locales folder.

这个问题的答案,为什么Android的行为这种方式,可以在官方文件活动说:

The answer to why Android behaves this way, can be found in the official documentation for the Activity saying:

[...]

这是因为任何应用程序的资源,包括布局文件,可以更改基于任何配置值完成。因此,要处理配置更改的唯一安全的方法是重新获取所有资源,包括布局,图形和字符串。由于活动必须已经知道如何保存其状态,并从该状态重新创建自己,这是有一个活动的启动本身就带有新配置的简便方法。

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

这家伙写的教程还跟整个教程项目添加为一个下载,所以我建议你去看看,看看它是如何工作的,因为它工作;-)您可以outcomment的 onConfigurationChanged 方法,因为它似乎并不能做任何事情。

The guy writing the tutorial was kind enough to add the whole tutorial project as a download, so I recommend you go check it out to see how it's working, because it is working ;-) You can outcomment the onConfigurationChanged method, as it doesn't seem to be doing anything.

希望这有助于。

这篇关于地点:onConfigurationChanged不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆