我們都知道在Android上可以通過WebView加載網(wǎng)頁,但是能否加載html的源碼呢?答案是肯定的。一般條件下,我們可以在WebView上加載html的代碼。
在WebView上加載html文件
WebView wView = (WebView)findViewById(R.id.wv1);
WebSettings wSet = wView.getSettings();
wSet.setJavaScriptEnabled(true);
// 打開本包內(nèi)asset目錄下的index.html文件
wView.loadUrl("file:///android_asset/index.html");
// 打開本地sd卡內(nèi)的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");