Android How to Display Links in webview like this?

余生颓废 提交于 2020-01-02 03:00:50

问题


Does anyone know of a way to Display URL's in webview like this

But here I am geting Like this here no focus and no click option not working any thing give me some sugessions please..

this code useing display data in webview

 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadDataWithBaseURL(null, string, "text/html", "utf-8", null);

回答1:


String str="Your text goes here... http://stackoverflow.com/questions/14576507/android-how-to-display-links-in-webview-like-this";

ArrayList<String> myArray = new ArrayList<String>();

myArray.add( "<!DOCTYPE HTML><!-- created HTML PAGE -->");
myArray.add("<head> ");
myArray.add("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"  width=100%>");
myArray.add("<meta name=\"viewport\" content=\"width=device-width\">");
myArray.add("<style>");
myArray.add("   p { font-family:Arial, Helvetica, sans-serif;}");
myArray.add("</style>");
myArray.add("</head> ");
myArray.add("<body style=\"background-color: transparent; margin-left:5%; margin-right:5%; \">");

myArray.add("<div >");
Spannable sp = new SpannableString(str);
Linkify.addLinks(sp, Linkify.ALL);
str = Html.toHtml(sp) ;
myArray.add(str);

String myFullString = myArray.toString().replace("[", "").replace("]", "").replace(",", "\n").replace("&lt;", "<").replace("&gt;", ">");

mWebView.loadDataWithBaseURL("about:blank", myFullString ,"text/html", "utf-8", null);



回答2:


Use Linkify.

Spannable sp = new SpannableString(Html.fromHtml(string));
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
webView.loadData(html, "text/html", "utf-8");



回答3:


Sample code:

Java:

 TextView t2 = (TextView) findViewById(R.id.text2);
 t2.setMovementMethod(LinkMovementMethod.getInstance());

android:

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/yourid"
android:id="@+id/yourid"
android:layout_below="@+id/yourid" android:layout_centerInParent="true"
android:layout_marginTop="20dp"></TextView>

Android WebView: Change Auto-Link Display for webview




回答4:


Please Add android:autoLink="web" in your TextView

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_text"
        android:autoLink="web"  
        android:textColorLink="@color/link"
        android:text="click here to load www.fb.com"
        android:textColor="@color/black" />



回答5:


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 an 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/14576507/android-how-to-display-links-in-webview-like-this

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