【问题标题】:How can I load an HTML template into a WebView?如何将 HTML 模板加载到 WebView 中?
【发布时间】:2011-08-31 04:56:18
【问题描述】:

想象一个如下所示的 HTML 模板:

<html>
<head>
    <style type="text/css">
        body {
            color: white;
        }
    </style>
</head>
<body>
The changes include:
<br/><br/>
%1$s
<br/><br/>
Would you like to update now?
</body>
</html>

我知道我可以使用webView.loadUrl("file:///android_asset/html/upgrade.html") 将内容加载到 WebView 中。但我也想在运行时替换 $1,就像我使用 String.format() 从 XML 文件加载它一样。

是否可以先将文件内容加载到字符串中,以便我可以在其上运行String.format(),然后改用webView.loadData()

【问题讨论】:

    标签: android webview


    【解决方案1】:

    我想出了一个基于this answer to a similar question的解决方案:

    String prompt = "";
    AssetManager assetManager = getApplicationContext().getResources().getAssets();
    try {
        InputStream inputStream = assetManager.open("html/upgrade-alert.html");
        byte[] b = new byte[inputStream.available()];
        inputStream.read(b);
        prompt = String.format(new String(b),changes);
        inputStream.close();
    } catch (IOException e) {
        Log.e(LOGTAG, "Couldn't open upgrade-alert.html", e);
    }
    
    WebView html = new WebView(this);
    html.loadData(prompt,"text/html","utf-8");
    

    【讨论】:

      【解决方案2】:

      只需在某个字符串中写入整个 html 替换一个要替换的字符串,然后使用 loadData 加载。

      http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/WebView1.html

      http://sriram-iyengar.blogspot.com/2011/05/android-webview-loaddata-and-encoding.html

      类似的东西

      【讨论】:

      • 我不想将所有的 HTML 都塞进代码中的字符串中——我更愿意将我的文本和 HTML 保存在资源文件中。我尝试将它放在 strings.xml 中,但您必须转义所有括号和引号,这很乱。
      【解决方案3】:

      您可以使用 HTMLCleaner 从您的 WebView 中获取 HTML 作为 DOM,并通过 TagNodes 更改它。一个如何使用 HTMLCLeaner 获取节点并给出它的某个属性的示例。当然,您应该在 AsyncTask 中执行此操作,以免阻塞 ui。

      public static String snapFromCleanedHTMLWithXPath(String stringURL, String xPath, String attrToStrip) {
          String snap = "";
      
          // create an instance of HtmlCleaner
          HtmlCleaner cleaner = new HtmlCleaner();
      
          // take default cleaner properties
          CleanerProperties props = cleaner.getProperties();
      
          props.setAllowHtmlInsideAttributes(true);
          props.setAllowMultiWordAttributes(true);
          props.setRecognizeUnicodeChars(true);
          props.setOmitComments(true);
      
          // open a connection to the desired URL
          URL url;
          try {
              url = new URL(stringURL);
              URLConnection conn = url.openConnection();
      
              // use the cleaner to "clean" the HTML and return it as a TagNode object
              TagNode root = cleaner.clean(new InputStreamReader(conn.getInputStream()));
      
              Object[] foundNodes = root.evaluateXPath(xPath);
      
              if (foundNodes.length > 0) {
                  // casted to a TagNode
                  TagNode foundNode = (TagNode) foundNodes[0];
                  snap = foundNode.getAttributeByName(attrToStrip);
              }
          } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (XPatherException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          cleaner = null;
          props = null;
          url = null;
          return snap;
      }
      

      HTMLCLeaner-Website

      【讨论】:

      • 谢谢@einschnaehkeee。这是一种有趣的方法,尽管对我的需求来说感觉有点复杂。基本上我只是希望能够将文件的内容读入一个字符串,这样我就可以对其进行字符串替换。
      • 嗯,也许你可以用这种方法实现你想要的:lexandera.com/2009/01/injecting-javascript-into-a-webview我希望这会有所帮助。^^
      猜你喜欢
      • 2011-07-19
      • 2023-04-03
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多