简体   繁体   English

string.xml 中的 Android 引用字符串

[英]Android reference string in string.xml

Is it possible to reference a string in strings.xml是否可以在 strings.xml 中引用字符串

Eg:例如:

<string name="application_name">@string/first_name Browser</string>
<string name="first_name">Chrome</string>

Where depending on requirements, i can switch the value of first_name to "Chrome", "Firefox" or "Opera".根据要求,我可以将 first_name 的值切换为“Chrome”、“Firefox”或“Opera”。

You can give the reference of string resource, but limitation are as follows您可以给出字符串资源的参考,但限制如下

<string name="first_name">Chrome</string>
<string name="application_name">@string/first_name</string> // gives "Chrome"
<string name="application_name">Chrome @string/first_name</string> // gives "Chrome @string/first_name"
<string name="application_name">@string/first_name Chrome </string> // gives error

If content starts with "@" then Android considers this is a referenced string, see last case which gives an error because Android's tools take @ and the next string to it as the string's reference name, it will try to find a resource called "@string/first_name Chrome" which doesn't exist.如果内容以“@”开头,那么 Android 认为这是一个引用字符串,请参见最后一个给出错误的案例,因为 Android 的工具将 @ 和下一个字符串作为字符串的引用名称,它会尝试找到一个名为“@”的资源string/first_name Chrome" 不存在。

You can use String Format to dynamically assign sub-strings like <string name="application_name">%1$s browser</string>您可以使用字符串格式来动态分配子字符串,例如<string name="application_name">%1$s browser</string>

to use使用

String text = String.format(res.getString(R.string.application_name), "Chrome");

Yes, you can do so without having to add any Java/Kotlin code, using this small library that allows you to do so using XML only at buildtime.是的,您可以这样做,而无需添加任何 Java/Kotlin 代码,使用这个允许您仅在构建时使用 XML 的小型库。 So for your case, you'd have to set up your strings like this:因此,对于您的情况,您必须像这样设置字符串:

<string name="application_name">${first_name} Browser</string>
<string name="first_name">Chrome</string>

And then after running the gradle plugin, you'll get this:然后在运行 gradle 插件之后,你会得到这个:

<!-- Auto generated during compilation -->
<string name="application_name">Chrome Browser</string>

This is the link to the library: https://github.com/LikeTheSalad/android-stem这是图书馆的链接: https ://github.com/LikeTheSalad/android-stem

Disclaimer: I'm the author of this library.免责声明:我是这个库的作者。

The Strings in the strings.xml are fixed and cannot be changed at run time. strings.xml 中的字符串是固定的,不能在运行时更改。 You will have to define a string for each case, and do the switch in the code.您必须为每种情况定义一个字符串,并在代码中进行切换。

String name;

if (/* browser is Chrome*/) {
    name = getString(R.string.first_name_chrome);
} else if (/* browser is Firefox */) {
    name = getString(R.string.first_name_firefox);
}

You can however make the application select the correct string for different languages automatically.但是,您可以让应用程序自动为不同的语言选择正确的字符串。 This can be done by placing string files in localized folders (values-en, values-fr, values-pl etc).这可以通过将字符串文件放在本地化文件夹(values-en、values-fr、values-pl 等)中来完成。

You can read more about localization at http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/您可以在http://www.icalocalize.com/site/tutorials/android-application-localization-tutorial/阅读有关本地化的更多信息

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

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