我想从我的strings.xml文件中的另一个字符串引用一个字符串,如下所示(特别注意“message_text”字符串内容的结尾):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_text">Add item</string>
    <string name="message_text">You don't have any items yet! Add one by pressing the \'@string/button_text\' button.</string>
</resources>

我已经尝试了上面的语法,但然后文本打印出“@string/button_text”作为明文。不是我想要的。我希望消息文本打印“您还没有任何项目!”按‘添加项目’按钮添加一个。”

有什么已知的方法可以达到我想要的吗?

理由是: 我的应用程序有一个项目列表,但当该列表为空时,我显示一个“@android:id/空”TextView代替。TextView中的文本通知用户如何添加新项。我想让我的布局万无一失的变化(是的,我是傻瓜的问题:-)


当前回答

我已经创建了一个小库,允许您在构建时解析这些占位符,因此您不必添加任何Java/Kotlin代码来实现您想要的结果。

基于你的例子,你必须像这样设置你的字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_text">Add item</string>
    <string name="message_text">You don't have any items yet! Add one by pressing the ${button_text} button.</string>
</resources>

然后插件将负责生成以下内容:

<!-- Auto generated during compilation -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="message_text">You don't have any items yet! Add one by pressing the Add item button.</string>
</resources>

它也适用于本地化和风味字符串,生成的字符串将保持自己更新,每当你改变你的模板或它的值。

更多信息请访问:https://github.com/LikeTheSalad/android-stem

其他回答

你可以使用你自己的逻辑,递归地解析嵌套的字符串。

/**
 * Regex that matches a resource string such as <code>@string/a-b_c1</code>.
 */
private static final String REGEX_RESOURCE_STRING = "@string/([A-Za-z0-9-_]*)";

/** Name of the resource type "string" as in <code>@string/...</code> */
private static final String DEF_TYPE_STRING = "string";

/**
 * Recursively replaces resources such as <code>@string/abc</code> with
 * their localized values from the app's resource strings (e.g.
 * <code>strings.xml</code>) within a <code>source</code> string.
 * 
 * Also works recursively, that is, when a resource contains another
 * resource that contains another resource, etc.
 * 
 * @param source
 * @return <code>source</code> with replaced resources (if they exist)
 */
public static String replaceResourceStrings(Context context, String source) {
    // Recursively resolve strings
    Pattern p = Pattern.compile(REGEX_RESOURCE_STRING);
    Matcher m = p.matcher(source);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String stringFromResources = getStringByName(context, m.group(1));
        if (stringFromResources == null) {
            Log.w(Constants.LOG,
                    "No String resource found for ID \"" + m.group(1)
                            + "\" while inserting resources");
            /*
             * No need to try to load from defaults, android is trying that
             * for us. If we're here, the resource does not exist. Just
             * return its ID.
             */
            stringFromResources = m.group(1);
        }
        m.appendReplacement(sb, // Recurse
                replaceResourceStrings(context, stringFromResources));
    }
    m.appendTail(sb);
    return sb.toString();
}

/**
 * Returns the string value of a string resource (e.g. defined in
 * <code>values.xml</code>).
 * 
 * @param name
 * @return the value of the string resource or <code>null</code> if no
 *         resource found for id
 */
public static String getStringByName(Context context, String name) {
    int resourceId = getResourceId(context, DEF_TYPE_STRING, name);
    if (resourceId != 0) {
        return context.getString(resourceId);
    } else {
        return null;
    }
}

/**
 * Finds the numeric id of a string resource (e.g. defined in
 * <code>values.xml</code>).
 * 
 * @param defType
 *            Optional default resource type to find, if "type/" is not
 *            included in the name. Can be null to require an explicit type.
 * 
 * @param name
 *            the name of the desired resource
 * @return the associated resource identifier. Returns 0 if no such resource
 *         was found. (0 is not a valid resource ID.)
 */
private static int getResourceId(Context context, String defType,
        String name) {
    return context.getResources().getIdentifier(name, defType,
            context.getPackageName());
}

例如,从一个活动中调用它

replaceResourceStrings(this, getString(R.string.message_text));

我创建了一个简单的gradle插件,允许你从另一个字符串引用一个字符串。您可以引用在另一个文件中定义的字符串,例如在不同的构建变体或库中。这种方法的缺点——IDE重构不会找到这样的引用。

使用{{string_name}}语法引用字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="super">Super</string>
    <string name="app_name">My {{super}} App</string>
    <string name="app_description">Name of my application is: {{app_name}}</string>
</resources>

要集成插件,只需将下一个代码添加到应用程序或库模块级构建中。gradle文件

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.android-text-resolver:buildSrc:1.2.0"
  }
}

apply plugin: "com.icesmith.androidtextresolver"

更新: 该库不支持Android gradle插件3.0及以上版本,因为该插件的新版本使用aapt2,它将资源打包为.flat二进制格式,因此打包的资源对该库不可用。作为临时解决方案,您可以通过设置android禁用aapt2。在gradle中enableAapt2=false。属性文件。

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

更新:

你甚至可以定义你的实体全局,例如:

res /生/ entities.ent:

<!ENTITY appname "MyAppName">
<!ENTITY author "MrGreen">

res /价值/ string.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
    <!ENTITY % ents SYSTEM "./res/raw/entities.ent">
    %ents;   
]>

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

除了以上Francesco Laurita的回答https://stackoverflow.com/a/39870268/9400836

似乎有一个编译错误“&entity被引用,但未声明”,可以通过引用外部声明来解决,就像这样

res - raw entities耳鼻喉科。

<!ENTITY appname "My App Name">

res /价值/ strings.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
    <!ENTITY appname SYSTEM "/raw/entities.ent">
]>
<resources>
    <string name="app_name">&appname;</string>
</resources

虽然它编译和运行,但它有一个空值。也许有人知道怎么解决。我会发表评论,但最低声誉是50。

我认为你不能。但是你可以根据自己的喜好“格式化”字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_text">Add item</string>
    <string name="message_text">You don't have any items yet! Add one by pressing the %1$s button.</string>
</resources>

在代码中:

Resources res = getResources();
String text = String.format(res.getString(R.string.message_text),
                            res.getString(R.string.button_text));