1

我的文件夹中有一个header.html文件和一个footer.html文件assets/html/

这是我创建和填充WebView内容的代码:

WebView contentView = (WebView) view.findViewById(R.id.contentView);
String contentText = Function.OBJECT.get("info").toString();

contentView.loadData(contextText, "text/html", null);

如何contextTextheader.htmland包装footer.html并在 ? 中显示输出WebView

4

1 回答 1

0

如果我理解正确,您想解析 header.html 和 footer.html 并在两者之间放置一些内容。

通过使用 WebView.loaddata() 您告诉 WebView 使用字符串作为内容,但这不是您想要的。

制作 index.html、添加页眉/页脚 div、通过 Javascript/Jquery 将 header.html/footer.html 中的内容绑定/附加到这些 div(jquery 将外部 html 文件附加到我的页面)并使用 WebView 更容易。 loadUrl() 从资产中加载新的 index.html:

public class MainActivity extends ActionBarActivity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    String url  =("file:///android_asset/index.html");

    mWebView.loadUrl(url);
    }
}
于 2015-05-15T15:29:34.620 回答