删除android webview中的页眉和页脚

时间:2015-04-28 17:06:17

标签: android webview

我正在尝试在webview组件中加载响应式网页。但我想剥离网页的页眉和页脚,只加载网页的正文。如何在Android webview中实现这一目标。

<div class="header-container">
<header class="header clearfix" ng-include="'/modules/core/homepage/header-partial.html'"></header>
</div>

2 个答案:

答案 0 :(得分:2)

我能够删除页眉和页脚并使用loadDataWithBaseURL()方法加载网页

Document document = Jsoup.connect(mUrl).get();
document.getElementsByClass("header-container").remove();
document.getElementsByClass("footer").remove();
WebSettings ws = mWebView.getSettings();
ws.setJavaScriptEnabled(true);
//mWebView.loadData(document.toString(),"text/html","utf-8");
mWebView.loadDataWithBaseURL(mUrl,document.toString(),"text/html","utf-8","");

根据开发者文档:

请注意,JavaScript的相同原始策略意味着在使用此方法加载的页面中运行的脚本将无法访问使用“数据”以外的任何方案加载的内容,包括“http(s)”。要避免此限制,请将loadDataWithBaseURL()与适当的基本URL一起使用。

http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String,java.lang.String,java.lang.String)

答案 1 :(得分:1)

如果你想编辑html,我建议使用像jsoup这样的html解析器。他们删除页眉和页脚,最后将数据加载到WebView

try {

    // Load the html into jsoup
    Document doc = Jsoup.connect("http://your-site.com/").get();

    // find and remove header
    Element header = doc.getElementById("your-header");
    header.remove();

    // find and remove footer
    Element footer = doc.getElementById("your-footer");
    footer.remove();

    // Load data into a WebView
    WebView wv = (WebView) findViewById(R.id.webView);
    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    wv.loadData(doc.toString(), "text/html", "utf-8");

} catch (IOException e) {
    e.printStackTrace();
}