ITEEDU

通过WebView嵌入浏览器

1)允许internet连接的访问

 

在上面的例子中,我们可能发现无法相关的网页,这是因为程序不具备internet连接的权限, 我们需要在Androidmanifest.xml中为App设置相关的权限。

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

2)在Android XML中设置WebView控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout .... >
    <WebView android:id="@+id/c131_webkit"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
</LinearLayout>

3)通过WebView对象,加载某个URL

WebView browser=(WebView)findViewById(R.id.c131_webkit);
browser.loadUrl("http://commonsware.com");

我们注意到嵌入的 browser没有工具栏的,因此需要通过程序来某些操作和处理,例如reload(), goBack(), canGoBack(), goForward(), canGoForward(), goBackOrForward(),负数表示回跳N次,正数表示前行N次,canGoBackOrForward(), clearCache(), clearHistory()。

4)通过WebView对象,加载自己的内容

我们可以将WebView作为自己的布局框,在 里面使用HTML来表述自己的内容。

browser.loadData("<html><body><h2>Hello , world!</h2></body></html>", "text/html", "UTF-8");