我在strings.xml文件中写了以下内容:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>

我得到了以下错误:

The reference to entity "Drop" must end with the ';' delimiter.

我怎么能写字符&在字符串。xml?


当前回答

对于特殊字符,我通常使用Unicode定义,例如“&”,如果我是正确的:\u0026。这里有一个很好的参考页面:http://jrgraphix.net/research/unicode_blocks.php?block=0

其他回答

应该是这样的:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&amp;Drop</string>

&在字符串中:

<string name="string_abc">Translate &amp; Scan Objects</string>

输出将是:

翻译和扫描对象

如果您想直接在xml中硬编码字符串,可以通过这种方式将其相加。

  <TextView 
     android:layout_marginStart="5dp"
     android:layout_marginLeft="5dp"
     android:layout_centerVertical="true"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="You &amp; Me"

我发现的最简单的方法是将“&”替换为对应的Unicode。对于上面给出的例子,写如下:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag\u0026Drop</string>

为了避免错误,使用提取字符串:

<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>