简体   繁体   English

使用其他资源文件启动android应用程序

[英]Launch an android application with different resource file

I have an application which can be launched in 2 versions. 我有一个可以在2个版本中启动的应用程序。

The main difference between the 2 application are the resource file. 这两个应用程序之间的主要区别是资源文件。 (Some parts are renamed, but the application handle only one language) (某些部分已重命名,但应用程序仅处理一种语言)

Is it possible to switch between 2 strings.xml file with @string/hello or R.string.hello with some specific settings in android ? 是否可以在Android中使用某些特定设置在@ string / hello或R.string.hello的2个string.xml文件之间切换? If not, what is the best approach to that problem ? 如果没有,那么解决该问题的最佳方法是什么? I imagined that I can use some eclipse feature to switch the xml file if there is some parameter ... I prefer have only apk package but it isn't mandatory. 我以为如果有一些参数,我可以使用一些eclipse功能来切换xml文件...我更喜欢只有apk包,但这不是强制性的。

I can't change the language on the client ... 我无法在客户端上更改语言...

Regards 问候

Edit 编辑

good idea Aleks. 好主意阿列克斯。 I refactored the projet into a library and launched it with good locale parameters. 我将projet重构为一个库,并使用良好的语言环境参数启动了它。 In that case I can have 3 differents project with the good database. 在那种情况下,我可以拥有3个具有良好数据库的项目。

If I understand correctly, you want to be able to switch the language of the application without changing the language on the phone. 如果我理解正确,则希望能够在不更改电话语言的情况下切换应用程序的语言。 I suppose, I can see a scenario where this can be useful. 我想,我可以看到一个有用的场景。

You could try to do something like this. 您可以尝试做这样的事情。

1) Build your app with multiple string resources just like you would if you were to support multiple locales/languages. 1)使用多个字符串资源来构建您的应用程序,就像您要支持多种语言环境/语言一样。

2) In your manifest, add configChanges="locale" to your activity: 2)在清单中,将configChanges="locale"添加到您的活动中:

<activity android:name=".Main" android:configChanges="locale" android:label="@string/app_name" />

3) The first time the app starts, it will be in the language of the client phone. 3)首次启动应用程序时,它将使用客户端电话的语言。 When the user changes the language in your app, save the new language in whatever way you prefer (sqlite, file, app bundle, etc.) and force-restart the app. 当用户在您的应用程序中更改语言时,以您喜欢的任何方式(sqlite,文件,应用程序包等)保存新语言,然后强制重新启动应用程序。

4) When your app is starting, retrieve the saved language and set default Locale to it: 4)应用启动时,检索保存的语言并将其设置为默认语言环境:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //load saved language
        String languageToUse  = ...
        if(languageToUse != null)
        {
            Locale locale = new Locale(languageToLoad); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        }
        this.setContentView(R.layout.main);
    }

I haven't tried this myself, so this is just a general idea - see if it works for you. 我自己还没有尝试过,所以这只是一个总体思路-看看它是否对您有用。

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

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