我想从我的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中的文本通知用户如何添加新项。我想让我的布局万无一失的变化(是的,我是傻瓜的问题:-)
你可以使用你自己的逻辑,递归地解析嵌套的字符串。
/**
* 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。属性文件。
我已经创建了一个小库,允许您在构建时解析这些占位符,因此您不必添加任何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
只要整个字符串由引用名称组成,就可以在另一个字符串中引用一个。例如,这将工作:
<string name="app_name">My App</string>
<string name="activity_title">@string/app_name</string>
<string name="message_title">@string/app_name</string>
它甚至在设置默认值时更有用:
<string name="string1">String 1</string>
<string name="string2">String 2</string>
<string name="string3">String 3</string>
<string name="string_default">@string/string1</string>
现在,您可以在代码中的任何地方使用string_default,并且可以随时轻松更改默认值。