Android WebView控件基本使用示例
Android WebView用于在 android 中顯示網(wǎng)頁??梢詮南嗤膽?yīng)用程序或 URL 加載網(wǎng)頁。它用于在 android 活動中顯示在線內(nèi)容。
Android WebView 使用 webkit 引擎來顯示網(wǎng)頁。
android.webkit.WebView 是 AbsoluteLayout 類的子類。
Android WebView 類的loadUrl()和loadData()方法用于加載和顯示網(wǎng)頁。
Android WebView 示例
讓我們看看使用 Web 視圖顯示 baidu.com 網(wǎng)頁的簡單代碼。
WebView mywebview = (WebView) findViewById(R.id.webView1); mywebview.loadUrl("http://www.baidu.com/");
讓我們看看使用 Web 視圖顯示 HTML 網(wǎng)頁的簡單代碼。在這種情況下,html 文件必須位于資產(chǎn)目錄中。
WebView mywebview = (WebView) findViewById(R.id.webView1); mywebview.loadUrl("file:///android_asset/myresource.html");
讓我們看另一個顯示字符串的 HTML 代碼的代碼。
String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>"; mywebview.loadData(data, "text/html", "UTF-8");
完整的 Android WebView 示例
讓我們看一個完整的 Android WebView 示例。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
要在應(yīng)用程序中本地添加網(wǎng)頁(.html、.jsp),需要將它們放置在 assets 文件夾中。資產(chǎn)文件夾創(chuàng)建為:右鍵單擊應(yīng)用程序 -> 新建 -> 文件夾 -> 資產(chǎn)文件夾 -> 主目錄,或者簡單地在主目錄中創(chuàng)建資產(chǎn)目錄。
MainActivity.java
package com.example.webview; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView mywebview = (WebView) findViewById(R.id.webView); mywebview.loadUrl("http://www.baidu.com"); //系統(tǒng)默認會通過手機瀏覽器打開網(wǎng)頁,為了能夠直接通過WebView顯示網(wǎng)頁,則必須設(shè)置 mywebview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //使用WebView加載顯示url view.loadUrl(url); //返回true return true; } }); /* String data = "<html><body><h1>Hello, World!</h1></body></html>"; mywebview.loadData(data, "text/html", "UTF-8");*/ //mywebview.loadUrl("file:///android_asset/myresource.html"); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.webview"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:usesCleartextTraffic="true" android:theme="@style/Theme.WebView"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.WebView.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
輸出:
如果加載 HTML 頁面,讓我們看看輸出。
如果您加載 baidu.com 網(wǎng)頁,讓我們看看輸出。
總結(jié)
到此這篇關(guān)于Android WebView控件基本使用示例的文章就介紹到這了,更多相關(guān)Android WebView控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析Android開發(fā)優(yōu)化之:對界面UI的優(yōu)化詳解(二)
在一個應(yīng)用程序中,一般都會存在多個Activity,每個Activity對應(yīng)著一個UI布局文件。一般來說,為了保持不同窗口之間的風格統(tǒng)一,在這些UI布局文件中,幾乎肯定會用到很多相同的布局2013-05-05Android GridView中包含EditText的焦點重新獲取方法
這篇文章主要介紹了Android GridView中包含EditText的焦點重新獲取方法,實例分析了界面刷新時EditText重新獲取焦點的技巧,需要的朋友可以參考下2016-03-03ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06Android開發(fā)Jetpack組件DataBinding用例詳解
這篇文章主要為大家介紹了Android開發(fā)Jetpack組件DataBinding的使案用例詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02android使用surfaceview+MediaPlayer播放視頻
這篇文章主要為大家詳細介紹了android使用surfaceview+MediaPlayer播放視頻,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11詳解Android Service與Activity之間通信的幾種方式
這篇文章主要介紹了詳解Android Service與Activity之間通信的幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04