Android使用webView加載html頁面的詳細(xì)步驟
1、首先在布局xml里面指定WebView根節(jié)點(diǎn)
<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緩存,當(dāng)H5網(wǎng)頁使用localstorage時(shí)一定要設(shè)置 myWebSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù) myWebSettings.setDatabaseEnabled(true); // 解決加載本地內(nèi)存中報(bào)錯(cuò) err_access_denied myWebSettings.setAllowFileAccess(true); myWebSettings.setAllowContentAccess(true); // 解決webView報(bào)錯(cuò) Loading local files from file:// urls is not possible due browser security restrictions /** * 設(shè)置是否允許運(yùn)行在一個(gè)file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容, * 包括其他file schema URLs。 * 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源, * 包括其他的文件和http,https等其他的源。與上面的類似,實(shí)現(xiàn)一個(gè)就可以。 * webSetting.setAllowUniversalAccessFromFileURLs(true); * */ myWebSettings.setAllowUniversalAccessFromFileURLs(true); /** * 設(shè)置是否允許運(yùn)行在一個(gè)file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容, * 包括其他file schema URLs。 * 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源, * 包括其他的文件和http,https等其他的源。與上面的類似,實(shí)現(xiàn)一個(gè)就可以。 */ //myWebSettings.setAllowUniversalAccessFromFileURLs(true); //加載html if (filePath != null) { myWebView.loadUrl(urlPath); } }
3、創(chuàng)建assets目錄(與res目錄同一級(jí)別)
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)核信息并上報(bào)服務(wù)器,服務(wù)器返回結(jié)果決定使用哪個(gè)內(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)核加載失敗,會(huì)自動(dòng)切換到系統(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緩存,當(dāng)H5網(wǎng)頁使用localstorage時(shí)一定要設(shè)置 webSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù) webSettings.setDatabaseEnabled(true); // 解決加載本地內(nèi)存中報(bào)錯(cuò) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Win10+Android+夜神安卓模擬器,搭建ReactNative開發(fā)環(huán)境
今天小編就為大家分享一篇關(guān)于使用Win10+Android+夜神安卓模擬器,搭建ReactNative開發(fā)環(huán)境,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10Android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼
這篇文章主要介紹了 android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05android判斷設(shè)備是否有相機(jī)的實(shí)例代碼
下面小編就為大家?guī)硪黄猘ndroid判斷設(shè)備是否有相機(jī)的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Android開發(fā)基礎(chǔ)之創(chuàng)建啟動(dòng)界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎(chǔ)之創(chuàng)建啟動(dòng)界面Splash Screen的方法,以實(shí)例形式較為詳細(xì)的分析了Android定制啟動(dòng)界面的布局及功能實(shí)現(xiàn)相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)類似360,QQ管家那樣的懸浮窗
用到的就是WindowManager以及WindowManager.LayoutParams,對(duì)這個(gè)LayoutParams做文章,當(dāng)設(shè)置為屬性后,然后,創(chuàng)建一個(gè)View,將這個(gè)View添加到WindowManager中就行2013-06-06android studio錯(cuò)誤: 常量字符串過長(zhǎng)的解決方式
這篇文章主要介紹了android studio錯(cuò)誤: 常量字符串過長(zhǎng)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解
這篇文章主要為大家介紹了Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android自定義View繪制貝塞爾曲線中小紅點(diǎn)的方法
貝塞爾曲線的本質(zhì)是通過數(shù)學(xué)計(jì)算的公式來繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學(xué)公式和驗(yàn)證,那些偉大的數(shù)學(xué)家已經(jīng)證明過了,所以就只講講Android開發(fā)中的運(yùn)用吧2023-02-02