我可以在 string.xml 的字符串资源中使用字符串资源吗?

发布于 2024-12-02 00:53:27 字数 580 浏览 1 评论 0原文

正如标题所示,我的问题是我不知道是否/如何可以在 string.xml 中的字符串资源中使用字符串资源。

<string name="projectname">Include Test</string>
<string name="about">@string/projectname is to test if you can use a String Resource in a String Resource</string>

我的问题是是否有人知道我是否可以写,

<string name="identifier">@string/other_identifier</string>

因为 Eclispe 说

error: Error: No resource found that matches the given name (at 'other_identifier' with value '@string/other_identifier eingeben...').

As the title says, my problem is that I don't know If/How I can use a String Resource in a String Resource in string.xml.

<string name="projectname">Include Test</string>
<string name="about">@string/projectname is to test if you can use a String Resource in a String Resource</string>

My question is if anyone knows if I can write

<string name="identifier">@string/other_identifier</string>

because Eclispe says

error: Error: No resource found that matches the given name (at 'other_identifier' with value '@string/other_identifier eingeben...').

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

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

发布评论

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

评论(5

花之痕靓丽 2024-12-09 00:53:27

您最好在运行时构造这些String。在String.format的帮助下将不同的String资源组合在一起。

更多详细信息:http://developer.android.com/guide/主题/资源/string-resource.html#FormattingAndStyling

You are better off constructing these Strings at runtime. Combine different String resources together with the help of String.format.

Further details: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

浮萍、无处依 2024-12-09 00:53:27

不,我认为这是不可能的。

No, I don't think that is possible.

演出会有结束 2024-12-09 00:53:27

编辑:实际上这个解决方案,使用自定义ENTITY条目是很多更好。


以下内容应该是开箱即用的:

<string name="identifier">@string/other_identifier</string>

我刚刚使用 API 级别 8 进行了尝试,效果很好。另请参阅 https://stackoverflow.com/a/6378421/786434

能够在其他单词中交叉引用字符串,您可以自己创建一个小辅助方法:

private static Pattern pattern = Pattern.compile("@string/(\\S+)");

public static String getString(Resources res, int stringID) {
String s = res.getString(stringID);

Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
    try {
    final Field f = R.string.class.getDeclaredField(matcher.group(1));
    final int id = f.getInt(null);
    final String replace = res.getString(id);
    s = s.replaceAll(matcher.group(), replace);

    } catch (SecurityException ignore) {
    } catch (NoSuchFieldException ignore) {
    } catch (IllegalArgumentException ignore) {
    } catch (IllegalAccessException ignore) {
    }
}

return s;
}

Edit: Actually this solution, using custom ENTITY entries is much nicer.


The following should work out of the box:

<string name="identifier">@string/other_identifier</string>

I just tried it with API Level 8 and it works fine. See also https://stackoverflow.com/a/6378421/786434

To be able to cross-reference strings within other words, you can create yourself a small helper method:

private static Pattern pattern = Pattern.compile("@string/(\\S+)");

public static String getString(Resources res, int stringID) {
String s = res.getString(stringID);

Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
    try {
    final Field f = R.string.class.getDeclaredField(matcher.group(1));
    final int id = f.getInt(null);
    final String replace = res.getString(id);
    s = s.replaceAll(matcher.group(), replace);

    } catch (SecurityException ignore) {
    } catch (NoSuchFieldException ignore) {
    } catch (IllegalArgumentException ignore) {
    } catch (IllegalAccessException ignore) {
    }
}

return s;
}
聚集的泪 2024-12-09 00:53:27

在 xml 中插入常用字符串(例如应用程序名称)而不使用 Java 代码的好方法此处

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY appname "MyAppName">
<!ENTITY author "MrGreen">
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>

A nice way to insert a frequently used string (e.g. app name) in xml without using Java code here:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY appname "MyAppName">
<!ENTITY author "MrGreen">
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>
七月上 2024-12-09 00:53:27

是的,您可以 :) 虽然不是原生的,但您可以使用这个小型库,它允许您在构建时解析这些占位符,因此您无需添加任何 Java/Kotlin 代码即可使其工作。

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

<string name="projectname">Include Test</string>
<string name="about">${projectname} is to test if you can use a String Resource in a String Resource</string>

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

<!-- Auto generated during compilation -->
<string name="about">Include Test is to test if you can use a String Resource in a String Resource</string>

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

免责声明:我是该库的作者。

Yes you can :) Not natively though, but you can use 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 strings like this:

<string name="projectname">Include Test</string>
<string name="about">${projectname} is to test if you can use a String Resource in a String Resource</string>

And then the plugin will take care of creating the following:

<!-- Auto generated during compilation -->
<string name="about">Include Test is to test if you can use a String Resource in a String Resource</string>

More info here: https://github.com/LikeTheSalad/android-stem

Disclaimer: I'm the author of that library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文