Android Studio/XML 特殊字符编码

Android Studio/XML Special characters encoding

我正在使用 XML 处理布局文件,我有一个 TextView,它必须包含几个特殊字符,如“‡”、"oe"、“ƒ”...(甚至我的浏览器未能正确显示它们,所以我不知道你是否正确)

如何在XML中写入这些字符??

它们会在 android 设备中正确显示吗??

使用xml保留这些unicode字符串:

Note: you don't have to use all languages below, if you only need one of them, it is perfectly fine.

默认(英文),res/values/strings.xml,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Maps</string>
    <string name="open_in">Open in Google Maps</string>
</resources>

法语,res/values-fr/strings。xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Plans</string>
    <string name="open_in">Ouvrir dans Google Maps</string>
</resources>

日语,res/values-ja/strings。xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">マップ</string>
    <string name="open_in">Googleマップで開く</string>
</resources>

要在这些 xml 文件中使用字符串:

yourTextView.setText(R.string.title); 

您可以使用 html 代码将它们保存在 xml 中,但是,如果它们采用 UTF-8 表示法,则通常不需要这样做,实际字符也可以:

<string name="my_string">&#65;</string>

或者,您可以在 TextView 上使用 unicode 调用 setText():

textView.setText("\u266b");

如有需要,可参考此unicode table

你可以用html代码带特殊字符,这是我用来搜索特殊html代码列表的页面: https://dev.w3.org/html5/html-author/charref

奖励:如果你想将 setText 设置为带有来自 Java 代码的特殊字符的文本视图,你可以使用这个:

 Html.fromHtml("//html code here")

这样就可以了 textView.setText(Html.fromHtml("your special character text"));