【问题标题】:Directly put html code in a WebView (Android)直接把html代码放到一个WebView中(Android)
【发布时间】:2012-12-09 18:14:42
【问题描述】:

在使用 WebView 时,我们通常会为其添加一个 URL:

WebView.loadUrl(myURL);

但是可以直接放HTML代码吗??所以它的逻辑是:

WebView.loadContent ( <html><head><script></script></head><body>....</body></html> );

谢谢。

【问题讨论】:

    标签: android html webview


    【解决方案1】:

    看看这个: http://developer.android.com/reference/android/webkit/WebView.html

     // OR, you can also load from an HTML string:
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null);
    

    【讨论】:

    • 谢谢。你节省了我的时间:)。
    【解决方案2】:
     String yourhtmlpage = "<html><body>You scored <b>hello world</b> points.</body></html>";
     webview.loadDataWithBaseURL(null, yourhtmlpage, "text/html", "UTF-8", null);
    

    【讨论】:

    • baseURL 部分是干什么用的?
    • > 将给定数据加载到此 WebView 中,使用 baseUrl 作为内容的基本 URL。基本 URL 既用于解析相对 URL,也用于应用 JavaScript 的同源策略。 historyUrl 用于历史条目。
    【解决方案3】:

    试试这个代码。这个对我有用。

    WebSettings settings = mDesc.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
    mDesc.loadData(mDescText, "text/html; charset=utf-8",null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2015-01-27
      • 2019-12-29
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多