的WebView,本地.css文件到HTML页面中添加? [英] WebView, add local .CSS file to an HTML page?

查看:149
本文介绍了的WebView,本地.css文件到HTML页面中添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的我使用的WebView来显示网页至极从互联网上来自Apache使用的HttpClient我拿来的一部分。要只我想从HTML中的一部分,我用Jsoup。

In android i'm using WebView to display a part of a webpage wich I fetched from the internet using HttpClient from Apache. To only have the part I want from the html, I use Jsoup.

String htmlString = EntityUtils.toString(entity4); // full html as a string                                 
Document htmlDoc = Jsoup.parse(htmlString); // .. as a Jsoup Document
Elements tables = htmlDoc.getElementsByTag("table"); //important part

现在我只需要加载 tables.toString()在web视图,并显示。现在我想用这个页面链接一个CSS文件至极我保存在我的资产文件夹。我知道我能有这样的事情

Now I can just loadtables.toString() in the WebView and it displays. Now I want to link a CSS file wich I store inside my assets folder with this page. I know I can have something like

<LINK href="styles/file.css" type="text/css" rel="stylesheet">   

在我的HTML,但我怎么联系起来,以便它使用一个我已经存储在本地?

In my html, but how do I link it so It uses the one I've stored locally?

---编辑---
我现在已经改成这样:

---EDIT---
I've now changed to this:

StringBuilder sb = new StringBuilder();
    sb.append("<HTML><HEAD><LINK href=\"file:///android_asset/htmlstyles_default.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
    sb.append(tables.toString());
    sb.append("</body></HTML>");
    return sb.toString();

不知怎的,我没有得到应用到页面的样式。它是我用错了位置的路径?请帮助我..

Somehow I do not get the styles applied to the page. Is it the location path I used that is wrong? please help me ..

推荐答案

塞瓦·阿列克谢耶夫是正确的,你应该存储在资产CSS文件文件夹,但参照文件:///android_asset/filename.css URL不为我工作。

Seva Alekseyev is right, you should store CSS files in assets folder, but referring by file:///android_asset/filename.css URL doesn't working for me.

还有另一种解决方案:把CSS在资产文件夹,做你的操作使用HTML,但引用CSS通过相对路径,然后负载HTML来的WebView通过<一个href="http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29"><$c$c>loadDataWithBaseURL()方法:

There is another solution: put CSS in assets folder, do your manipulation with HTML, but refer to CSS by relative path, and load HTML to WebView by loadDataWithBaseURL() method:

webView.loadDataWithBaseURL("file:///android_asset/", htmlString, "text/html", "utf-8", null);

例如。你有 styles.css的文件,把它放到资产文件夹,创建HTML并将其加载:

E.g. you have styles.css file, put it to assets folder, create HTML and load it:

StringBuilder sb = new StringBuilder();
sb.append("<HTML><HEAD><LINK href=\"styles.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
sb.append(tables.toString());
sb.append("</body></HTML>");
webView.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html", "utf-8", null);

P.S。我来这个解决方案的感谢<一href="http://stackoverflow.com/questions/4114496/load-html-file-to-webview-with-custom-css/4114753#4114753">Peter Knego的回答。

这篇关于的WebView,本地.css文件到HTML页面中添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆