从strings.xml中的另一个字符串引用一个字符串?(Reference one string from another string in strings.xml?)

编程入门 行业动态 更新时间:2024-10-28 06:30:53
从strings.xml中的另一个字符串引用一个字符串?(Reference one string from another string in strings.xml?)

我想从我的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中的文本是通知用户如何添加新项目。 我想让我的布局傻瓜改变(是的,我是傻瓜问题:-)

I would like to reference a string from another string in my strings.xml file, like below (specifically note the end of the "message_text" string content):

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

I've tried the above syntax but then the text prints out the "@string/button_text" as clear text. Not what I want. I would like the message text to print "You don't have any items yet! Add one by pressing the 'Add item' button."

Is there any known way to achieve what I want?

RATIONALE: My application has a list of items, but when that list is empty I show a "@android:id/empty" TextView instead. The text in that TextView is to inform the user how to add a new item. I would like to make my layout fool-proof to changes (yes, I'm the fool in question :-)

最满意答案

在xml中插入常用字符串(例如应用程序名称)而不使用Java代码的好方法: 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>

A nice way to insert a frequently used string (e.g. app name) in xml without using Java code: 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>

UPDATE:

You can even define your entity globaly e.g:

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>

更多推荐

本文发布于:2023-07-23 04:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   xml   strings   Reference   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!