我是Android编程的新手,我想让我们jqMath在WebView中显示一些数学公式。
这是我的代码:
WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
String js = "<html><head>"
+ "<link rel='stylesheet' href='jqmath-0.4.0.css'>"
+ "<script src='jquery-1.4.3.min.js'></script>"
+ "<script src='jqmath-etc-0.4.0.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js, "text/html", "UTF-8");这个代码有什么问题?
更新--我的问题已经解决了,但是如果其他人有同样的问题,我也会将loadData函数改为loadDataWithBaseURL,以供参考。
发布于 2014-03-02 12:16:25
您需要为css、js文件正确指定路径如下:
WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
String path="file:///android_asset/";
String js = "<html><head>"
+ "<link rel='stylesheet' href='"+path+"jqmath-0.4.0.css'>"
+ "<script src='"+path+"jquery-1.4.3.min.js'></script>"
+ "<script src='"+path+"jqmath-etc-0.4.0.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js, "text/html", "UTF-8");请注意,文件应位于“资产”文件夹中。
发布于 2014-03-02 12:13:39
假设您将js lib放在资产/ js中,那么js在资产文件夹中的位置与src声明不匹配。
发布于 2015-04-01 21:54:06
在指定了像Khalid所说的路径之后,下面的内容对我起了作用
webView.loadDataWithBaseURL( "file:///android_asset/" ,js, "text/html", "UTF-8", null);而不是webView.loadData方法。
https://stackoverflow.com/questions/22127355
复制相似问题