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

发布于 2024-09-19 07:22:06 字数 818 浏览 9 评论 0 原文

我想知道是否有办法将 XML 文件中定义的 元素插入/注入到另一个 元素中,这样做只是使用 XML。

例如,我可以:

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

并且我正在寻找类似的内容:

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

以便 getString(R.string.about_application) 将导致“作者:Francesco”。

我知道我可以使用 String.format(string, formatArgs) 在 Java 代码中组合这两个元素,例如:

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

然后在代码中使用,

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

但我想直接在 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.

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".

I know that I could combine the two elements in Java code using String.format(string, formatArgs)like for example:

<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.

Can anyone suggest me a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泡沫很甜 2024-09-26 07:22:15

是的,这是可能的,我创建了这个小型库,允许您在构建时解析这些占位符,因此您无需添加任何 Java/Kotlin 代码即可使其工作。

根据您的示例,您必须像这样设置字符串:

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

然后插件将负责创建以下内容:

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

更多信息在此处:https://github.com/LikeTheSalad/android-stem

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.

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

那小子欠揍 2024-09-26 07:22:14

Gradle 插件 0.10 带来了他们所谓的 ma​​nifestPlaceholders ,它基本上可以满足您的需要,但此功能目前仅在 AndroidManifest 中工作。虽然有 针对下一个 Android 构建版本 1.4 出现的问题1.3 目前处于 beta4 阶段,因此应该是接近 RC1,希望很快就能看到 1.4 beta1)

这个问题应该扩展 xml 配置文件中的占位符(我只是祈祷这将包括字符串文件,而不仅仅是基本的 xml 配置)。

来自http://tools.android.com/技术文档/新建系统

对于自定义占位符替换,请使用以下 DSL
配置占位符值:

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

将替换以下声明中的占位符:

android:label="${activityLabel}" >

迫不及待地想尝试一下。这样,您可以在字符串文件中的多次出现中放置一个占位符,并仅在一个位置定义该值,而不是修改所有 java 文件以添加带有 %1$s 的参数。

目前,唯一干净的解决方案虽然是实体技巧,但这个如果您想覆盖flavor中的值,则不起作用,因为实体必须在同一个xml文件中定义。

Gradle plugin 0.10 brings what they call manifestPlaceholders which basically does what you need but this feature currently only work in the 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).

This issue should expand the placeholders in xml configurations files (I just pray this will include strings file and not only basic xml configuration).

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

For custom placeholders replacements, use the following DSL to
configure the placeholders values :

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

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.

烟酒忠诚 2024-09-26 07:22:13

不幸的是我认为这是不可能的。我不久前问了一个类似的问题,并且被告知这是不可能的。

Unfortunately I don't think that is possible. I asked a similar question a while ago, and was told it wasn't possible.

复古式 2024-09-26 07:22:11

如果我明白您想要做什么,那么内部(已解析)一般实体 可能会帮助您实现您正在寻找的目标。

示例说明如何将值“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>

当 XML 解析器读取时,文档将被解析和评估,或“被视为”,如下所示:

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

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:

<?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 version="1.0"?>
<doc>
  <string name="author">Francesco</string>
  <string name="about_application">Author: Francesco</string>
</doc>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文