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

可紊 提交于 2019-12-11 07:19:21

问题


The source HTML string (including the symbol) is coming from the strings.xml resource file, and is destined to be displayed in a 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 "®" (i.e. an "A" circumflex, followed by the registered trademark symbol) - i.e. two characters are shown, the first incorrect & unwanted.
  • I see the same result when using the entity reference, &#174;
  • Using &reg; fails, and the project does not build.

This is the code that pushes the string resource into the 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");

I pushed the HTML string to Logcat, and it looks fine - it shows the symbol correctly. So if the string is ok, and the WebView is set to use UTF-8, why is the symbol not displaying correctly?

UPDATE I tested on other devices. I can only reproduce this issue on a Galaxy Nexus on Android v4.04. 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. I've changed the question title to clarify this is an ICS issue.


回答1:


String resources are not designed to hold arbitrary HTML, including arbitrary entity references.

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; after your call to getString().

At the end of the day, you need to get &reg; to WebView. If you cannot determine a way to do that with a string resource, you will need to store this value someplace else.




回答2:


You are using loadData(html, "text/html", "UTF-8"); This method expect a html string in your variable html. But it is not. Try

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

for instance.

--updated to have a full html document

And if you store it in a resource file use :

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



回答3:


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). 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.



来源:https://stackoverflow.com/questions/10106278/display-registered-trademark-symbol-in-android-v4-0-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!