简体   繁体   English

Android-更改语言环境(语言)

[英]Android - Change Locale (Language)

I'm trying to add language controller meaning changing the language of my app. 我正在尝试添加语言控制器,这意味着更改应用程序的语言。

I've added 4 flags (ImageView), and whenever I press the flag I want, I want the app to change the language depennding on that flag. 我添加了4个标记(ImageView),每当我按下所需的标记时,我都希望应用程序更改该标记所依赖的语言。

主菜单

The apps start out being English, and when I press the Danish falg, the language DOES switch to Danish, but whenever I want to change back to english, nothing happens. 这些应用程序最初是英语,当我按丹麦语falg时,语言会切换为丹麦语,但是每当我想改回英语时,都不会发生任何事情。

I've made several strings.xml files in their own value folder 我已经在自己的value文件夹中制作了几个strings.xml文件

value-en/strings.xml
value-dk/strings.xml

Method changing language: 方法更改语言:

english.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config,getResources().getDisplayMetrics());
            setContentView(R.layout.activity_main);
    }
});

english is my variable for the ImageView english是我的ImageView变量

I found my solution. 我找到了解决方案。

In order to save the onClickListener() on my other components, I need to refresh the activity, posted below: 为了将onClickListener()保存在其他组件上,我需要刷新活动,如下所示:

To restart the activity 重新开始活动

I simply made a method like the one below: 我只是简单地采用了以下方法:

//Restarts the activity after changing the languagse
private void RestartActivity(){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

And everytime I change the language, I run the method: 每当我更改语言时,我都会运行该方法:

english.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config,getResources().getDisplayMetrics());
        setContentView(R.layout.activity_main);
        RestartActivity(); //Run the method as the last thing
    }
});

For some reason the listeners doesn't see mto stay, but this solution works for me. 由于某些原因,听众看不到mto停留,但是此解决方案对我有用。

I tested below code and it is working as per my consideration. 我测试了下面的代码,并且按我的考虑工作。 Check this it will help you or not. 选中此选项将对您有所帮助。 Call this method on your click listener and pass locale string as a parameter. 在您的点击监听器上调用此方法,并将区域设置字符串作为参数传递。

private void setDeviceDefaultLanguage(String languageToLoad) { 私人无效setDeviceDefaultLanguage(String languageToLoad){

    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config,
            getResources().getDisplayMetrics());
    Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM