我想在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>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了上面的语法,然后文本打印出"@ string/button_text"作为明文.不是我想要的.我想要打印消息文本"你还没有任何项目!按"添加项目"按钮添加一个项目."
有没有任何已知的方法来实现我想要的?
RATIONALE:
我的应用程序有一个项目列表,但是当该列表为空时,我会显示"@android:id/empty"TextView.TextView中的文本是通知用户如何添加新项目.我想让我的布局变得傻瓜变化(是的,我是个傻瓜:-)
我正在按照此解决方案在我的字符串资源文件中使用enetities:
是否可以直接在Android资源XML文件中进行字符串替换?
我在资源树中使用外部文件:/res/raw/entities.dtd,其内容:
<!ENTITY ent_devicename "MyDeviceName">
Run Code Online (Sandbox Code Playgroud)
在string.xml资源文件中:
<!DOCTYPE resources [
<!ENTITY % ent_devicename SYSTEM "../raw/entities.dtd">
%ent_devicename;
]>
<resources>
<string name="name">The name is &ent_devicename;</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
实体"ent_devicename"已被引用,但未被声明.
如您所见,Android Studio可识别外部实体文件:
和实体:
有人能提供一个正确的例子来使事情有效吗?我的意思是一个完全可编译的Android Studio项目,在单独的文件中包含实体声明.
更新 好的,如果你更加关注这个w3schools链接:
https://www.w3schools.com/xml/xml_dtd_entities.asp
你看到了解决方案:
外部entities.dtd文件包含
<!ENTITY ent_devicename "MyDeviceName">
Run Code Online (Sandbox Code Playgroud)
那么新的字符串资源资源:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY ent_devicename SYSTEM "../raw/entities.dtd">
]>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="Typos">
<string name="ent_devicenamxxe2">&ent_devicename;</string>
Run Code Online (Sandbox Code Playgroud)
我改变了<!ENTITY % ent_devicename对<!ENTITY ent_devicename(不再%)我删除%ent_devicename;
现在它编译但是生成的APK似乎忽略了实体值(使用空字符串).所以问题没有解决!
让我知道!
xml dtd android-resources android-studio android-gradle-plugin
我试图了解如何使用外部实体,但我遗漏了一些东西.
我有一个第一个文件test_entity.xml
<?xml version="1.0" encoding="UTF-8" ?>
<test>
<test_1>Inside</test_1>
</test>
Run Code Online (Sandbox Code Playgroud)
第二个文件是test_entity2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE root [
<!ENTITY test_entity SYSTEM "/Users/username/test_entity.xml">
<!ENTITY test_string "This is a test">
]>
<root>
<tmp id="1">&test_entity;</tmp>
<tmp id="2">&test_string;</tmp>
</root>
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试将test_entity.xml内容嵌入到test_entity2.xml中,但输出如下所示:
<root>
<tmp id="1"/>
<tmp id="2">This is a test</tmp>
</root>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
更新
我通过firefox和chrome显示test_entity2.xml