如何在Android应用中管理区域配置

时间:2019-02-23 00:15:41

标签: android android-resources

基于客户的国家/地区设置(他们在应用程序中设置),我需要使用三种区域配置之一。例如:选择要命中的区域云端点。

Android支持多个resource.xml文件,但它们是根据语言环境选择的。但就我而言,我想根据客户设置选择它们。

我可以在唯一的resources.xml文件中使用所有端点配置,如下所示:

<resources>
    <string name="my_endpoint_us">https://usa-endpoint.mysite.com</string>
    <string name="my_endpoint_eu">https://europe-endpoint.mysite.com</string>
    <string name="my_endpoint_apj">https://asia-endpoint.mysite.com</string>
</resources>

并根据客户设置制定配置名称,但看起来很笨拙:

myResources.getString(
    it.getIdentifier("my_endpoint" + customer.region, "string", "com.mypackage")
)

1 个答案:

答案 0 :(得分:0)

您可以尝试将区域的值作为字符串资源的参数传递

<string name="my_endpoint">https://%1$s-endpoint.mysite.com</string>

然后像这样获取端点的值

String endpoint = getString(R.string.my_endpoint, customer.region)

因此在这里,您的 strings.xml 文件中只有一个字符串,但是如果每个区域的端点都没有该密钥,则这里是弱点。

相关问题