简体   繁体   English

Android:如何注入<string>元素到另一个<string> XML中的元素?

[英]Android: How to inject a <string> element into another <string> element in XML?

I would like to know whether there is a way to insert/inject a <string> element defined in an XML file into another <string> element, doing that just with XML.我想知道是否有一种方法可以将 XML 文件中定义的<string>元素插入/注入到另一个<string>元素中,只需使用 XML 即可。

For example I could have:例如我可以有:

<string name="author">Francesco</string>`

and I am looking for something like:我正在寻找类似的东西:

<string name="about_application">Author: @string/author</string>`

so that getString(R.string.about_application) would result in "Author: Francesco".这样getString(R.string.about_application)将导致“作者:Francesco”。

I know that I could combine the two elements in Java code using String.format(string, formatArgs) like for example:我知道我可以使用String.format(string, formatArgs)在 Java 代码中组合这两个元素,例如:

<string name="author">Francesco</string>
<string name="about_application">Author: %1$s</string>`

and then in code use然后在代码中使用

String.format(getString(R.string.about_application), getString(R.string.author))

but I would like to do it in XML directly.但我想直接用 XML 来做。

Can anyone suggest me a way to do it?任何人都可以建议我一种方法吗?

If I understand what you are looking to do, then internal (parsed) general entities might help you achieve what you are looking for.如果我了解您要做什么,那么内部(解析的)一般实体可能会帮助您实现所需的目标。

An example of how you can define the value "Francesco" as an entity called "auth" and then use it in your XML:如何将值“Francesco”定义为名为“auth”的实体,然后在 XML 中使用它的示例:

<?xml version="1.0"?>
<!DOCTYPE doc [
  <!ENTITY auth "Francesco">
]>
<doc>
  <string name="author">&auth;</string>
  <string name="about_application">Author: &auth;</string>
</doc>

When read by an XML parser, the document will be parsed and evaluated, or "seen", as:当被 XML 解析器读取时,文档将被解析和评估,或“看到”,如下所示:

<?xml version="1.0"?>
<doc>
  <string name="author">Francesco</string>
  <string name="about_application">Author: Francesco</string>
</doc>

Unfortunately I don't think that is possible.不幸的是,我认为这是不可能的。 I asked a similar question a while ago, and was told it wasn't possible.不久前我问了一个类似的问题,并被告知这是不可能的。

Gradle plugin 0.10 brings what they call manifestPlaceholders which basically does what you need but this feature currently only work in the AndroidManifest. Gradle 插件 0.10 带来了他们所谓的manifestPlaceholders ,它基本上可以满足您的需求,但此功能目前仅适用于 AndroidManifest。 There is although an issue opened targeting the next Android build version 1.4 ( 1.3 is currently in beta4 so should be near RC1 and could see a 1.4 beta1 soon hopefully) .虽然针对下一个 Android 构建版本 1.4 存在一个问题1.3 目前处于 beta4 中,因此应该接近 RC1,并且有望很快看到 1.4 beta1)

This issue should expand the placeholders in xml configurations files ( I just pray this will include strings file and not only basic xml configuration ).这个问题应该扩展 xml 配置文件中的占位符(我只是祈祷这将包括字符串文件,而不仅仅是基本的 xml 配置)。

From : http://tools.android.com/tech-docs/new-build-system来自http ://tools.android.com/tech-docs/new-build-system

For custom placeholders replacements, use the following DSL to configure the placeholders values :对于自定义占位符替换,使用以下 DSL 来配置占位符值:

android {
        defaultConfig {
            manifestPlaceholders = [ activityLabel:"defaultName"]
        }
        productFlavors {
            free {
            }
            pro {
                manifestPlaceholders = [ activityLabel:"proName" ]
            }
        }

will substitute the placeholder in the following declaration :将替换以下声明中的占位符:

<activity android:name=".MainActivity"> android:label="${activityLabel}" >

Can't wait to try it out.迫不及待想尝试一下。 This way you could put a placeholder in multiple occurence in the strings file and define the value only in one place instead of modifying all java files to add an argument with %1$s这样,您可以在字符串文件中多次出现占位符并仅在一个位置定义值,而不是修改所有 java 文件以添加带有 %1$s 的参数

For now the only clean solution is although the entity trick but this will not work if you want to override the value in flavors since the entity must be defined in the same xml file.目前唯一干净的解决方案是实体技巧,但如果您想覆盖风味中的值,这将不起作用,因为实体必须在同一个 xml 文件中定义。

Yes it is possible, I've created this small library that allows you to resolve these placeholders at buildtime, so you won't have to add any Java/Kotlin code to make it work.是的,这是可能的,我创建了这个小型库,允许您在构建时解析这些占位符,因此您无需添加任何 Java/Kotlin 代码即可使其工作。

Based on your example, you'd have to set up your string like this:根据您的示例,您必须像这样设置字符串:

<string name="author">Francesco</string>
<string name="about_application">Author: ${author}</string>

And then the plugin will take care of creating the following:然后插件将负责创建以下内容:

<!-- Auto generated during compilation -->
<string name="about_application">Author: Francesco</string>

More info here: https://github.com/LikeTheSalad/android-stem更多信息在这里: https ://github.com/LikeTheSalad/android-stem

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

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