WebView的基本使用方法

WebView是View的子类,它主要用来显示网页的。

因为我们要用到网络,所以需要先添加权限:在 Anroidmanifest

<manifest ... > 
    <uses-permission        android:name="android.permission.INTERNET" /> 
    ... 
</manifest>

然后在布局文件写好布局,我写了一个填满整个屏幕的WebView

1 <?xml version="1.0" encoding="utf-8"?>
2     <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
3         android:id="@+id/webview"
4         android:layout_width="match_parent"
5         android:layout_height="match_parent" />

在界面中用 loadUrl()加载网页

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl(http://www.example.com);

到这里基本OK了。

上面的是加载网页的,这里是加载本地文件的,本地文件存放在:assets文件中。

myWebView.loadUrl(“file:///android_asset/XX.html“);  

这是加载HTML的字符串:

String htmlString = "<h1>Title</h1><p>This is HTML text<br /><i>Formatted in italics</i><br />Anothor Line</p>";
// 载入这个html页面
myWebView.loadData(htmlString, "text/html", "utf-8");
原文地址:https://www.cnblogs.com/huaqing-wkc/p/4936952.html