是否可以在Android中加载不同的string(xml)值 [英] Is it possible to load different string(xml) values in Android

查看:316
本文介绍了是否可以在Android中加载不同的string(xml)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想从xml加载不同的字符串值.几乎喜欢不同的语言.我有多个客户端访问同一个应用程序,但是他们想要不同的标签和样式.(例如,我有一个待办事项列表选项卡.客户端A希望将其称为日常任务".客户端B希望将其称为任务".客户端C希望获得活动列表.所有3个客户端具有相同的功能,但需要不同的文本最好.我不想为每个客户端创建一个新的应用程序,这将是一场噩梦,难以维护等等,而且我也不想每次我想要更改标签文本时都做出if语句.理想情况下,我想要的是为每个客户端加载不同字符串的单个应用程序.

In my app i want to load different string values from xml. Almost like the different language. I have multiple clients accessing the same app but they want different labels and styles. (An example would be I have a to do list tab. Client A wants it called day to day task. Client B wants it called task. Client C wants activity list. All 3 clients have the same functionality but want different text Preferably. I don't want to create a new app for each client it would be a nightmare to maintain etc and I also don't want to make an if statement every time i want a the labels text changed. Ideally what I want is single application that loads a different strings for each client.

推荐答案

您可以为每个客户端创建一个不同的buildType和或productFlavor.对于每个buildType/productFlavor,您可以定义一个单独的源集.这意味着每个buildType/productFlavor可以包含一个不同的strings.xml.字符串的密钥必须相同,但是每个客户端在buildType/productFlavor中可以有不同的字符串文件.

You could create a different buildType and or productFlavor for each client. For each buildType/productFlavor you can define a separate sourceset. This means that each buildType/productFlavor can contain a different strings.xml. The keys for the strings have to be the same, but each client can have a different string file within its buildType/productFlavor.

有关buildTypes/productFlavors的更多信息:

More information about buildTypes/productFlavors:

http://工具.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

http://developer.android.com/tools/building/configuring-gradle.html

例如您定义了两种口味:

e.g. you define two flavors:

android {
...
defaultConfig { ... }
signingConfigs { ... }
buildTypes { ... }
productFlavors {
    clientA {
    }
    clientB {
    }
  }
}

然后,您可以为每个客户在项目中创建一个源集:

Then you can create for each customer a sourceset in your project:

src/
 |-> main (contains the common code/strings)
 |-> clientA
 |        |-> res
 |            |-> values
 |                |-> strings.xml
 |-> clientB
          |-> res
              |-> values
                  |-> strings.xml

在每个strings.xml中,您可以为客户添加字符串.定义的字符串也将与主源集的字符串合并.

In each strings.xml you can add the strings for your customer. The defined strings will also be merged with the strings of the main sourceset.

您如何告诉您的应用程序使用什么SRC.之间主/客户A/客户B?

How do you tell your application what SRC to use. between Main/clientA/clientB?

您可以在Android Studio中进行构建,选择buildedType/productFlavor.

You can build select the builtType/productFlavor in Android Studio.

要处理具有特定风味的文件,请单击上的构建变体".IDE窗口的左侧,然后选择要在其中修改的样式Build Variants面板,如图2所示.AndroidStudio可能会显示源文件中的错误,而不是所选择的其中之一在Build Variants面板中,但这不会影响以下内容的结果构建.

To work on files from a particular flavor, click on Build Variants on the left of the IDE window and select the flavor you want to modify in the Build Variants panel, as shown in figure 2. Android Studio may show errors in source files from flavors other than the one selected in the Build Variants panel, but this does not affect the outcome of the build.

http://developer.android.com/tools/building/configuration-gradle.html#workBuildVariants

这篇关于是否可以在Android中加载不同的string(xml)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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