java 使用jsoup将自定义css添加到html代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5614352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add custom css to html code with jsoup
提问by PixelPW
I'm working on an Android app, which loads a HTML page and shows it in a webview. The problem is I want to add my custom css (the loaded HTML hasn't any CSS or link to a css). How do I add the custom css to the HTML code using jsoup? I cant modify the html. And how does the webview can open it afterwards? Thank you
我正在开发一个 Android 应用程序,它加载一个 HTML 页面并在 web 视图中显示它。问题是我想添加我的自定义 css(加载的 HTML 没有任何 CSS 或指向 css 的链接)。如何使用 jsoup 将自定义 css 添加到 HTML 代码?我无法修改html。然后webview如何打开它?谢谢
回答by BalusC
Several ways. You can use Element#append()
to append some piece of HTML to the element.
几种方式。您可以使用Element#append()
将一些 HTML 附加到元素。
Document document = Jsoup.connect(url).get();
Element head = document.head();
head.append("<link rel=\"stylesheet\" href=\"http://example.com/your.css\">");
Or, use Element#attr(name, value)
to add attributes to existing elements. Here's an example which adds style="color:pink;"
to all links.
或者,用于Element#attr(name, value)
向现有元素添加属性。这是一个添加style="color:pink;"
到所有链接的示例。
Document document = Jsoup.connect(url).get();
Elements links = document.select("a");
links.attr("style", "color:pink;");
Either way, after modification get the final HTML string by Document#html()
.
无论哪种方式,修改后都可以通过Document#html()
.
String html = document.html();
Write it to file by PrintWriter#write()
(with the right charset).
通过PrintWriter#write()
(使用正确的字符集)将其写入文件。
String charset = Jsoup.connect(url).response().charset();
// ...
Writer writer = new PrintWriter("/file.html", charset);
writer.write(html);
writer.close();
Finally open it in the webview. Since I can't tell it from top of head, here's just a link with an example which I think is helpful: WebViewDemo.java. I found the link on this blogby the way (which I in turn found by Google).
最后在webview中打开它。由于我无法从头顶上分辨出来,这里只是一个带有我认为有用的示例的链接:WebViewDemo.java。顺便说一下,我在这个博客上找到了链接(我又通过 Google找到了这个链接)。
回答by Jim Blackler
Probably the easiest way is to search and replace on the HTML text to insert your custom styles, before loading it into your WebView
. I do this in my app BBC News to restyle the news article page slightly. My code looks like this:
可能最简单的方法是搜索和替换 HTML 文本以插入自定义样式,然后再将其加载到WebView
. 我在我的应用程序 BBC News 中执行此操作以稍微重新设计新闻文章页面。我的代码如下所示:
text = text.replace("</head>",
"<style>h1 {font-size: x-large;} h1, div.date, div.storybody, img {margin:4px; padding:4px; line-height:1.25;}</style></head>");
See how I search and replace on the end head
tag (including my own </head>
tag in the replaced segment. This ensures that the new snippet goes in the right pace on the page.
看看我如何搜索和替换结束head
标记(包括我自己</head>
在替换段中的标记。这可确保新代码段在页面上以正确的速度运行。
回答by KilledKenny
There a a few ways to include ccs in html
有几种方法可以在 html 中包含 ccs
Tis i use if you have it stored as a external file:
如果您将其存储为外部文件,我会使用它:
<head><link rel="stylesheet" type="text/css" href="mystyle.css" /></head>
If You want to put it stight i the html file:
如果你想把它放在 html 文件中:
<head>
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>
Or if you wnat to modify a singel tag:
或者,如果您想修改单个标签:
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
*Edit
*编辑
Any of thees examples shouldn't have any problem whit displaying.
这些示例中的任何一个都不应该有任何显示问题。
Ref: W3 Schools CSS
参考:W3 学校 CSS