Android WebView: Change Auto-Link Display

南楼画角 提交于 2019-12-11 17:15:18

问题


Does anyone know of a way to modify the visual style of how the WebView displays auto-linked text (phone numbers, addresses, etc.)? Or more specifically, can I make the links that WebView detects look like standard clickable hyperlinks? For example,

webView.loadData("My phone number is 3035555555", "text/html", "utf-8");

This loads the text into the WebView and it is clickable, but it just looks like the rest of the body text. I also tried putting the text into an HTML file in assets and doing

webView.loadUrl("file:///android_asset/Test.html");

But that yielded the same result. Is there something in WebSettings or WebViewClient that controls this behavior I'm missing?

Cheers.


回答1:


You could do this to get what your looking for.

String header = "< ?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
String data = "< html>< body>< a href='tel:555-5599'>508-776-5510
" "< /body>< /html>";

mWebView.loadData(header+data, "text/html", "UTF-8");




回答2:


Try this.....

String header = "< ?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
String data = "< html>< body>< a href='tel:555-5599'>508-776-5510
" "< /body>< /html>";
mWebView.loadData(header+data,  "text/html", "UTF-8");

If you are loading a string of html texts into webView. Then you can use

mWebView.loadData(header+data, "text/html", "UTF-8");

If you have a html file. Then you can use

webView.loadUrl("file:///android_asset/mypage.html"):

Note: Dont forget to put your html file in your assets folder.

Cheers!!! :D



来源:https://stackoverflow.com/questions/4829247/android-webview-change-auto-link-display

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