开发者

Can I use a String Resource in a String Resource in string.xml?

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开发者_JAVA技巧_identifier eingeben...').


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


No, I don't think that is possible.


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;
}


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜