Android WebView控件基本使用示例
Android WebView用于在 android 中顯示網(wǎng)頁(yè)。可以從相同的應(yīng)用程序或 URL 加載網(wǎng)頁(yè)。它用于在 android 活動(dòng)中顯示在線(xiàn)內(nèi)容。
Android WebView 使用 webkit 引擎來(lái)顯示網(wǎng)頁(yè)。
android.webkit.WebView 是 AbsoluteLayout 類(lèi)的子類(lèi)。
Android WebView 類(lèi)的loadUrl()和loadData()方法用于加載和顯示網(wǎng)頁(yè)。
Android WebView 示例
讓我們看看使用 Web 視圖顯示 baidu.com 網(wǎng)頁(yè)的簡(jiǎn)單代碼。
WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.loadUrl("http://www.baidu.com/"); 讓我們看看使用 Web 視圖顯示 HTML 網(wǎng)頁(yè)的簡(jiǎn)單代碼。在這種情況下,html 文件必須位于資產(chǎn)目錄中。
WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.loadUrl("file:///android_asset/myresource.html"); 讓我們看另一個(gè)顯示字符串的 HTML 代碼的代碼。
String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>"; mywebview.loadData(data, "text/html", "UTF-8");
完整的 Android WebView 示例
讓我們看一個(gè)完整的 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)頁(yè)(.html、.jsp),需要將它們放置在 assets 文件夾中。資產(chǎn)文件夾創(chuàng)建為:右鍵單擊應(yīng)用程序 -> 新建 -> 文件夾 -> 資產(chǎn)文件夾 -> 主目錄,或者簡(jiǎ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)默認(rèn)會(huì)通過(guò)手機(jī)瀏覽器打開(kāi)網(wǎng)頁(yè),為了能夠直接通過(guò)WebView顯示網(wǎng)頁(yè),則必須設(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 頁(yè)面,讓我們看看輸出。

如果您加載 baidu.com 網(wǎng)頁(yè),讓我們看看輸出。

總結(jié)
到此這篇關(guān)于Android WebView控件基本使用示例的文章就介紹到這了,更多相關(guān)Android WebView控件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android WebView輸入框被檔問(wèn)題升級(jí)解析
- 淺談Android開(kāi)發(fā)Webview的Loading使用效果
- Android?webView加載數(shù)據(jù)時(shí)內(nèi)存溢出問(wèn)題及解決
- Android?WebView預(yù)渲染介紹
- Android?WebView軟鍵盤(pán)遮擋輸入框方案詳解
- Android WebView如何判斷是否滾動(dòng)到底部
- Android WebView基礎(chǔ)應(yīng)用詳解
- Android WebView實(shí)現(xiàn)全屏播放視頻
- Android?WebView緩存機(jī)制優(yōu)化加載慢問(wèn)題
相關(guān)文章
Android中TabLayout+ViewPager 簡(jiǎn)單實(shí)現(xiàn)app底部Tab導(dǎo)航欄
TabLayout 是Android com.android.support:design庫(kù)的一個(gè)控件。本文主要給大家介紹TabLayout+ViewPager 簡(jiǎn)單實(shí)現(xiàn)app底部Tab布局,需要的的朋友參考下2017-02-02
Android BottomNavigationView結(jié)合ViewPager實(shí)現(xiàn)底部導(dǎo)航欄步驟詳解
這篇文章主要介紹了Android BottomNavigationView結(jié)合ViewPager實(shí)現(xiàn)底部導(dǎo)航欄步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Android ListView的item背景色設(shè)置和item點(diǎn)擊無(wú)響應(yīng)的解決方法
在Android開(kāi)發(fā)中,listview控件是非常常用的控件,在大多數(shù)情況下,大家都會(huì)改掉listview的item默認(rèn)的外觀(guān)。2013-11-11
Android存儲(chǔ)訪(fǎng)問(wèn)框架的使用小結(jié)
這篇文章主要介紹了Android存儲(chǔ)訪(fǎng)問(wèn)框架的使用,存儲(chǔ)訪(fǎng)問(wèn)框架API和MediaStore?API的差異,在于存儲(chǔ)訪(fǎng)問(wèn)框架API,是基于系統(tǒng)文件選擇框的,用戶(hù)選擇了文件,那么相當(dāng)于授權(quán)了,?可以訪(fǎng)問(wèn)所有類(lèi)型的文件,需要的朋友可以參考下2022-01-01
解決Android加殼過(guò)程中mprotect調(diào)用失敗的原因分析
本文探討的主要內(nèi)容是mprotect調(diào)用失敗的根本原因,以及在加殼實(shí)現(xiàn)中的解決方案,通過(guò)本文的闡述,一方面能夠幫助遇到同類(lèi)問(wèn)題的小伙伴解決心中的疑惑,另一方面能夠給大家提供可落地的實(shí)現(xiàn)方案,需要的朋友可以參考下2022-01-01
kotlin實(shí)現(xiàn)通知欄提醒功能示例代碼
這篇文章主要給大家介紹了關(guān)于kotlin實(shí)現(xiàn)通知欄提醒功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android中TelephonyManager類(lèi)的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類(lèi)的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類(lèi)的使用技巧,需要的朋友可以參考下2015-09-09
Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問(wèn)題
本篇文章主要介紹了Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問(wèn)題,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能(類(lèi)似微信)
這篇文章主要為大家詳細(xì)介紹了Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能,類(lèi)似微信功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

