WebView的基本使用方法

 

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

由于咱们要用到网络,因此须要先添加权限:在 Anroidmanifestjava

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

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

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()加载网页web

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

 

到这里基本OK了。网络

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

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

这是加载HTML的字符串:spa

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");
相关文章
相关标签/搜索