在一个web视图自定义CSS渲染HTML [英] Rendering HTML in a WebView with custom CSS

查看:194
本文介绍了在一个web视图自定义CSS渲染HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用JSoup下载留言板网页的HTML(比方说,在这种情况下,它是一个页面,其中包含一个指定线程的职位)。我想借此HTML,去掉不需要的物品,并应用自定义CSS样式它是移动的web视图。

My app is using JSoup to download the HTML of a message board page (let's say in this case it is a page containing the posts of a given thread). I'd like to take this HTML, strip out unwanted items, and apply custom CSS to style it to be 'mobile' in a WebView.

我应该注入样式到HTML,因为我处理它(因为我无论如何都会被处理),或者是有一个CSS文件添加到我的应用程序的资产,只是指它的好方法。我想,后者将是理想的,但不知道该如何去做。

Should I inject the styles into the HTML as I process it (since I will be processing it anyway) or is there a good way to add a CSS file to my app's assets and simply refer to it. I figure the latter would be ideal, but unsure how to go about it.

我看到的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">loadDataWithBaseURL你可以参照当地的资产,但不知道如何利用它。

I see hints in WebView's loadDataWithBaseURL that you can refer to local assets, but not sure how to utilize it.

推荐答案

您可以使用<一个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">WebView.loadDataWithBaseURL

htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + htmlData;
// lets assume we have /assets/style.css file
webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "UTF-8", null);

和只有在那之后的WebView将能找到并使用CSS-文件从资产目录。

And only after that WebView will be able to find and use css-files from the assets directory.

PS是的,如果您将HTML文件形成的资产的文件夹,你并不需要指定一个基本URL。

ps And, yes, if you load your html-file form the assets folder, you don't need to specify a base url.

这篇关于在一个web视图自定义CSS渲染HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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