Android开发AndroidAndroid开发

Android加载html代码和文件

2016-08-14  本文已影响12177人  cooperise

我们都知道在Android上可以通过WebView加载网页,但是能否加载html的源码呢?答案是肯定的。一般条件下,我们可以在WebView上加载html的代码。

在WebView上加载html文件
WebView wView = (WebView)findViewById(R.id.wv1);
WebSettings wSet = wView.getSettings(); 
wSet.setJavaScriptEnabled(true); 
// 打开本包内asset目录下的index.html文件
wView.loadUrl("file:///android_asset/index.html"); 
// 打开本地sd卡内的index.html文件 
wView.loadUrl("content://com.android.htmlfileprovider/sdcard/index.html");
// 打开指定URL的html文件 
wView.loadUrl("http://wap.baidu.com"); 
在WebView上加载html代码
String content = "<p><font color='red'>hello baidu!</font></p>";
webview.loadData(content, "text/html", "UTF-8"); // 加载定义的代码,并设定编码格式和字符集。
在WebView上添加css样式
String linkCss = "<style type=\"text/css\"> " +
                     "img {" + 
                           "width:100%;" +  
                           "height:auto;" +  
                     "}" +  
                     "body {" +  
                           "margin-right:15px;" +  
                           "margin-left:15px;" +  
                           "margin-top:15px;" +  
                           "font-size:45px;" +  
                     "}" +  
                  "</style>";
String html = "<html><header>" + linkCss + "</header>" + content + "</body></html>"; 
webView.loadData(html, "text/html", "uft-8");
上一篇 下一篇

猜你喜欢

热点阅读