2012-05-29 41 views
2

我正在显示一个带有远程html内容的webview作为字符串从远程服务器获取。 我在本地存储了一个没有连接使用我的应用程序的HTML。使用本地css和js在WebView中加载HTML

此外,我还在本地存储.js脚本和.css样式文件。这些文件可以由服务器更新。

我所有这些文件存储在以下路径:

context.getFilesDir()+"content.css" 
context.getFilesDir()+"content.js" 

在HTML字符串,CSS和JS引用这样的:

<link rel="stylesheet" href="/content.css" type="text/css" media="screen">            
<script src="/content.js"></script> 

我加载使用

this.webView.loadDataWithBaseURL(getFilesDir().getAbsolutePath(), html, "text/html", "utf-8", "about:blank"); 
的HTML

但是风格和js都没有考虑到,所以我认为我们的路径出了问题e引用它们,或加载webView。那么怎么做呢?我发现许多使用“资产”文件夹的答案,但我不想使用它,因为我必须从服务器更新css和js。

回答

3

最后,我已经找到了解决办法:

public static void writeToFile(Context context, String content, String title) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(title,Context.MODE_WORLD_READABLE)); osw.write(content); osw.close(); }

  • 然后,我是指它在html:

    • 的CSS(或JS)文件使用此方法保存在本地使用文件

      <link rel="stylesheet" href="content.css" type="text/css" media="screen">
      <script src="content.js"></script>

    • 最后,我已经打开使用的WebView:

      this.webView = (WebView) findViewById(R.id.webview); this.webView.getSettings().setJavaScriptEnabled(true); this.webView.getSettings().setPluginsEnabled(true); this.webView.setHorizontalScrollBarEnabled(false); this.webView.setVerticalScrollBarEnabled(true); this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); this.webView.setWebViewClient(this.controller.getWebViewClient()); String basePath = "file://"+getFilesDir().getAbsolutePath()+"/"; this.webView.loadDataWithBaseURL(basePath, data, "text/html", "utf-8", "about:blank");

  • 相关问题