加载HTML文件的WebView与自定义CSS [英] Load HTML file to WebView with custom CSS

查看:348
本文介绍了加载HTML文件的WebView与自定义CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的WebView我的Andr​​oid应用程序,它负载(WebView.loadUrl <$ C C $>())从手机的内部存储不同的本地的HTML文件。我想包括一些自定义的CSS样式他们。

I have a WebView on my Android application which loads (WebView.loadUrl()) different local HTML files from phone's internal storage. I would like to include some custom css styles for them.

现在,我可以有我的应用程序编辑每一个HTML文件,并添加链接的CSS文件引用。

Now, I could have my app edit every HTML file and add linking reference for the CSS file.

我也可以读取该文件的内容,添加CSS链接,并使用 WebView.loadData()加载它。

I could also read the file contents, add the CSS linking and use WebView.loadData() to load it.

但有可能做到这一点变得更加简单,高效地?

But is it possible to do this a lot simpler and efficiently?

注意:的HTML文件从一个网站下载。所以手动编辑他们是不可能在此情况下,但一旦下载他们可以通过该应用,如果必要进行编辑。

Note: The HTML files are downloaded from a website. So editing them manually is not possible in this case, but once downloaded they can be edited via the app if necessary.

推荐答案

一种可能(我没有试过):

One possibility (I have not tried this):

<一个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"相对=nofollow> WebView.loadDataWithBaseURL(字符串的baseUrl,字符串数据,..)

的baseUrl 的文档使用,以解决相对URL。看看CSS的网址,构建的baseUrl 使CSS网址将引用本地CSS文件。

takes a baseURL for the document to use to resolve relative URLs. Take a look at the CSS url and construct baseURL so that CSS url will reference local CSS file.

这篇关于加载HTML文件的WebView与自定义CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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