我想引用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/ empty“TextView。该TextView中的文本用于通知用户如何添加新项。我想让我的布局变得简单易懂(是的,我就是那个有问题的笨蛋:-)
发布于 2016-10-05 17:29:43
在不使用Java代码的情况下,在xml中插入常用字符串(例如应用程序名称)的一种好方法:source
<?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/raw/entities.ent:
<!ENTITY appname "MyAppName">
<!ENTITY author "MrGreen">
res/values/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>
发布于 2011-06-17 04:50:05
只要您的整个字符串由引用名称组成,就可以在另一个字符串中引用一个。例如,这将会起作用:
<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
,并且可以随时轻松地更改默认值。
发布于 2011-01-20 18:47:13
我想你不能。但是你可以随心所欲地“格式化”一个字符串:
<?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));
https://stackoverflow.com/questions/4746058
复制相似问题