Android使用webView加載html頁面的詳細步驟
1、首先在布局xml里面指定WebView根節(jié)點
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>2、在.java的onCreate()里使用
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
//1. asset目錄下的index.html文件
String filePath = "file:///android_asset/html/index.html";
//2.本地內(nèi)存中的index.html文件
// 獲取文件夾路徑
String htmlPath = getExternalFilesDir("html").getPath();
File htmlFile = new File(htmlPath);
// 判斷是否存在,不存在則創(chuàng)建
if (htmlFile.exists()){
htmlPath = htmlFile.getPath()+File.separator+"index.html";
}else {
htmlFile.mkdirs();
htmlPath = htmlFile.getPath()+File.separator+"index.html";
}
// 地址
String localFilePath = "file:///"+htmlPath;
//3.指定的URL的html文件
/**
* 若是不顯示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
* 并且設(shè)置網(wǎng)絡(luò)權(quán)限
*/
String urlPath = "https://www.baidu.com/";
myWebView = findViewById(R.id.myWebView);
WebSettings myWebSettings = myWebView.getSettings();
// webView解決加載html頁面空白問題
myWebSettings.setJavaScriptEnabled(true);// 設(shè)置支持javascript
myWebSettings.setUseWideViewPort(true);//將圖片調(diào)整到適合webView大小
myWebSettings.setLoadWithOverviewMode(true);//縮放至屏幕大小
myWebSettings.setDomStorageEnabled(true);//設(shè)置DOM緩存,當H5網(wǎng)頁使用localstorage時一定要設(shè)置
myWebSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù)
myWebSettings.setDatabaseEnabled(true);
// 解決加載本地內(nèi)存中報錯 err_access_denied
myWebSettings.setAllowFileAccess(true);
myWebSettings.setAllowContentAccess(true);
// 解決webView報錯 Loading local files from file:// urls is not possible due browser security restrictions
/**
* 設(shè)置是否允許運行在一個file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容,
* 包括其他file schema URLs。
* 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源,
* 包括其他的文件和http,https等其他的源。與上面的類似,實現(xiàn)一個就可以。
* webSetting.setAllowUniversalAccessFromFileURLs(true);
* */
myWebSettings.setAllowUniversalAccessFromFileURLs(true);
/**
* 設(shè)置是否允許運行在一個file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容,
* 包括其他file schema URLs。
* 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源,
* 包括其他的文件和http,https等其他的源。與上面的類似,實現(xiàn)一個就可以。
*/
//myWebSettings.setAllowUniversalAccessFromFileURLs(true);
//加載html
if (filePath != null) {
myWebView.loadUrl(urlPath);
}
}3、創(chuàng)建assets目錄(與res目錄同一級別)

4、將要訪問的*.html頁面放置到assets目錄即可

5、使用X5內(nèi)核 騰訊SDK
地址:騰訊瀏覽服務(wù)
下載sdk:騰訊瀏覽服務(wù)-SDK下載
放置在libs文件夾,引用
AS高版本:
implementation(fileTree("libs"))AS低版本:
android{
...
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies{
...
compile files('libs/tbs_sdk_thirdapp_v4.3.0.386_44286_sharewithdownloadwithfile_withoutGame_obfs_20230210_114429.jar')
}
AndroidManifest.xml配置權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".activity.app.MyAplication"
***
/application>Application.java設(shè)置初始化
package com.example.yuanzhoulv.activity.app;;
import android.app.Application;
import com.tencent.smtt.sdk.QbSdk;
public class MyAplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//搜集本地tbs內(nèi)核信息并上報服務(wù)器,服務(wù)器返回結(jié)果決定使用哪個內(nèi)核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
// TODO Auto-generated method stub
//x5內(nèi)核初始化完成的回調(diào),為true表示x5內(nèi)核加載成功,否則表示x5內(nèi)核加載失敗,會自動切換到系統(tǒng)內(nèi)核。
}
@Override
public void onCoreInitFinished() {
// TODO Auto-generated method stub
}
};
//x5內(nèi)核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);
}
}
使用:
*.xml
<com.tencent.smtt.sdk.WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>*.java
//1. asset目錄下的index.html文件
String filePath = "file:///android_asset/html/index.html";
//2.本地內(nèi)存中的index.html文件
// 獲取文件夾路徑
String htmlPath = getExternalFilesDir("html").getPath();
File htmlFile = new File(htmlPath);
// 判斷是否存在,不存在則創(chuàng)建
if (htmlFile.exists()){
htmlPath = htmlFile.getPath()+File.separator+"index.html";
}else {
htmlFile.mkdirs();
htmlPath = htmlFile.getPath()+File.separator+"index.html";
}
// 地址
String localFilePath = "file:///"+htmlPath;
//3.指定的URL的html文件
/**
* 若是不顯示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
* 并且設(shè)置網(wǎng)絡(luò)權(quán)限
*/
String urlPath = "https://www.baidu.com/";
webView = findViewById(R.id.webView);
com.tencent.smtt.sdk.WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);// 設(shè)置支持javascript
webSettings.setUseWideViewPort(true);//將圖片調(diào)整到適合webView大小
webSettings.setLoadWithOverviewMode(true);//縮放至屏幕大小
webSettings.setDomStorageEnabled(true);//設(shè)置DOM緩存,當H5網(wǎng)頁使用localstorage時一定要設(shè)置
webSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù)
webSettings.setDatabaseEnabled(true);
// 解決加載本地內(nèi)存中報錯 err_access_denied
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
//加載html
if (filePath != null) {
webView.loadUrl(localFilePath);
}總結(jié)
到此這篇關(guān)于Android使用webView加載html頁面的文章就介紹到這了,更多相關(guān)Android用webView加載html頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Win10+Android+夜神安卓模擬器,搭建ReactNative開發(fā)環(huán)境
今天小編就為大家分享一篇關(guān)于使用Win10+Android+夜神安卓模擬器,搭建ReactNative開發(fā)環(huán)境,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼
這篇文章主要介紹了 android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法,以實例形式較為詳細的分析了Android定制啟動界面的布局及功能實現(xiàn)相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android實現(xiàn)類似360,QQ管家那樣的懸浮窗
用到的就是WindowManager以及WindowManager.LayoutParams,對這個LayoutParams做文章,當設(shè)置為屬性后,然后,創(chuàng)建一個View,將這個View添加到WindowManager中就行2013-06-06
android studio錯誤: 常量字符串過長的解決方式
這篇文章主要介紹了android studio錯誤: 常量字符串過長的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

