简体   繁体   English

在Android v4.0 WebView中显示®注册商标符号?

[英]Display ® registered trademark symbol in Android v4.0 WebView?

The source HTML string (including the symbol) is coming from the strings.xml resource file, and is destined to be displayed in a WebView. 源HTML字符串(包括符号)来自strings.xml资源文件,并注定要在WebView中显示。 I've tested with this in the resources: 我已经在资源中对此进行了测试:

<string name="MY_STRING">®</string>
  • Using the actual trademark symbol in the resources (®), the projects builds, but when displayed in the WebView it shows as "®" (ie an "A" circumflex, followed by the registered trademark symbol) - ie two characters are shown, the first incorrect & unwanted. 使用资源(®)中的实际商标符号构建项目,但是在WebView中显示时,项目显示为“®”(即“ A”抑音符,后跟注册商标符号)-即显示了两个字符,第一个不正确且不想要的内容。
  • I see the same result when using the entity reference, &#174; 使用实体引用&#174;时,我会看到相同的结果&#174;
  • Using &reg; 使用&reg; fails, and the project does not build. 失败,并且该项目无法生成。

This is the code that pushes the string resource into the WebView: 这是将字符串资源推送到WebView中的代码:

String html = getString(R.string.MY_STRING);
((WebView)findViewById(R.id.terms_web_view)).loadData(html, "text/html", "UTF-8");

I also tried this, but it did not help: webView.getSettings().setDefaultTextEncodingName("UTF-8"); 我也尝试过此方法,但没有帮助:webView.getSettings()。setDefaultTextEncodingName(“ UTF-8”);

I pushed the HTML string to Logcat, and it looks fine - it shows the symbol correctly. 我将HTML字符串推到Logcat,看起来不错-它正确显示了该符号。 So if the string is ok, and the WebView is set to use UTF-8, why is the symbol not displaying correctly? 因此,如果字符串正确,并且WebView设置为使用UTF-8,为什么符号不能正确显示?

UPDATE I tested on other devices. 我在其他设备上测试过的更新 I can only reproduce this issue on a Galaxy Nexus on Android v4.04. 我只能在Android v4.04上的Galaxy Nexus上重现此问题。 On a Nexus One v2.3.x, Wildfire S on v2.3.x and a Samsung Tab 10.1 on v3.2, it works fine. 在Nexus One v2.3.x,v2.3.x上的Wildfire S和v3.2上的Samsung Tab 10.1上,它可以正常工作。 I've changed the question title to clarify this is an ICS issue. 我更改了问题标题,以澄清这是ICS问题。

String resources are not designed to hold arbitrary HTML, including arbitrary entity references. 字符串资源并非旨在容纳任意HTML,包括任意实体引用。

You might be able to get an arbitrary entity reference to work if you pre-escape it: 也许能得到一个任意实体引用,如果你预先逃脱它的工作:

<string name="MY_STRING">&amp;reg;</string>

IIRC, that should decode to &reg; IIRC,应解码为&reg; after your call to getString() . 在调用getString()

At the end of the day, you need to get &reg; 最后,您需要获取&reg; to WebView . WebView If you cannot determine a way to do that with a string resource, you will need to store this value someplace else. 如果您无法确定使用字符串资源的方法,则需要将该值存储在其他位置。

You are using loadData(html, "text/html", "UTF-8"); 您正在使用loadData(html,“ text / html”,“ UTF-8”); This method expect a html string in your variable html. 此方法期望变量html中有一个html字符串。 But it is not. 但事实并非如此。 Try 尝试

String html = "<html><body>My text is ®</body></html>";

for instance. 例如。

--updated to have a full html document -更新为具有完整的html文档

And if you store it in a resource file use : 如果将其存储在资源文件中,请使用:

<string name="MY_STRING" formatted="false"><html><body>My text is ®</body></html></string>

This is not and Android developer answer but just something simple if you're trying to text somebody and neither Ⓡ nor ™ aren't available as symbols (like on my LG phone Android 7.0). 这不是Android开发人员的答案,但是如果您尝试向某人发送短信并且Ⓡ和™都不可用作符号(例如在我的LG手机Android 7.0上),则只是简单一些。 Just copy those symbols from this message thread and send them in a message to yourself or stuff them in a quick memo. 只需从此消息线程中复制这些符号,然后将其发送给自己,或将其塞入快速备忘录中即可。 Then you have them readily available for future use. 然后,您可以随时使用它们以备将来使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM