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)默認(rèn)會通過手機(jī)瀏覽器打開網(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中TabLayout+ViewPager 簡單實現(xiàn)app底部Tab導(dǎo)航欄
TabLayout 是Android com.android.support:design庫的一個控件。本文主要給大家介紹TabLayout+ViewPager 簡單實現(xiàn)app底部Tab布局,需要的的朋友參考下2017-02-02
Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導(dǎo)航欄步驟詳解
這篇文章主要介紹了Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導(dǎo)航欄步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Android ListView的item背景色設(shè)置和item點擊無響應(yīng)的解決方法
在Android開發(fā)中,listview控件是非常常用的控件,在大多數(shù)情況下,大家都會改掉listview的item默認(rèn)的外觀。2013-11-11
解決Android加殼過程中mprotect調(diào)用失敗的原因分析
本文探討的主要內(nèi)容是mprotect調(diào)用失敗的根本原因,以及在加殼實現(xiàn)中的解決方案,通過本文的闡述,一方面能夠幫助遇到同類問題的小伙伴解決心中的疑惑,另一方面能夠給大家提供可落地的實現(xiàn)方案,需要的朋友可以參考下2022-01-01
Android自定義View實現(xiàn)圓環(huán)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)圓環(huán)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09
Android中兩個Activity之間數(shù)據(jù)傳遞及返回問題
本篇文章主要介紹了Android中兩個Activity之間數(shù)據(jù)傳遞及返回問題,這里整理了詳細(xì)的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
Android仿iOS實現(xiàn)側(cè)滑返回功能(類似微信)
這篇文章主要為大家詳細(xì)介紹了Android仿iOS實現(xiàn)側(cè)滑返回功能,類似微信功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

