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");
相關文章
相關標籤/搜索