- 新增一個 Empty Activity 專案
- AndroidManifest.xml 加網路權限
<uses-permission android:name="android.permission.INTERNET" />
最終 AndroidManifest.xml 內容:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testwebview"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.TestWebView"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
- 開啟 layout 檔案 activity_main.xml,刪掉預設的 TextView(Hello World!),放一個 WebView
設定 id 為「myWebView」,layout_width、layout_height 屬性設為「0dp」
(使用專案預設的 ConstraintLayout)
WebView 屬性設定往下拉,找到 All Attributes 裡的 layout_constraints 設定
layout_constraintBottom_toBottomOf、layout_constraintEnd_toEndOf、layout_constraintStart_toStartOf、layout_constraintTop_toTopOf 都設為「parent」
最終 activity_main.xml 內容:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/myWebView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java 內容
package com.example.testwebview; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { WebView mWebView; WebSettings mWebSettings; //Android 9 (API level 28) 後,預設只能開啟 https 網站 String url = "https://google.com/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //mWebView = (WebView) findViewById(R.id.myWebView); mWebView = findViewById(R.id.myWebView); mWebSettings = mWebView.getSettings(); //mWebSettings.setUserAgentString("app"); mWebSettings.setJavaScriptEnabled(true);//開啟JS mWebSettings.setDomStorageEnabled(true);//開啟localStorage mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true); //若沒設定setWebViewClient(),則點WebView頁面的連結會呼叫系統瀏覽器開啟,不會保留在APP中 mWebView.setWebViewClient(new WebViewClient()); mWebView.loadUrl(url); } }
- 去除 APP 上方的 ActionBar 標題列,修改 res\values\themes.xml、res\values-night\themes.xml,改成 NoActionBar
themes.xml<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.TestWebView" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> </resources>
themes.xml(night)<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.TestWebView" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/purple_200</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/black</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_200</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> </resources>
[其他修改]
- Andriod WebView 瀏覽不安全(不受信任)的 HTTPS 憑證
https://xyz.cinc.biz/2022/03/andriod-webview-https.html - 改成可瀏覽明文沒加密的 HTTP 網站(Andriod WebView 出現 net::ERR_CLEARTEXT_NOT_PERMITTED 錯誤)
https://xyz.cinc.biz/2022/03/andriod-webview-http-net-err-cleartext-not-permitted.html - Andriod WebView 按手機返回鍵跳回桌面,改成回上一頁
https://xyz.cinc.biz/2022/03/andriod-webview-back-button-go-back-page.html - Andriod APP 按兩次返回鍵才確認跳回桌面
https://xyz.cinc.biz/2022/03/andriod-back-button-twice-confirm-exit.html - Android WebView「原生程式(Java)呼叫執行網頁JavaScipt」、「網頁JavaScipt呼叫執行原生程式(Java)」
https://xyz.cinc.biz/2022/04/android-webview-java-javascript-call-fun.html - Android WebView 連結 target 為 _blank 時,使用系統瀏覽器開啟新視窗
https://xyz.cinc.biz/2022/04/android-webview-link-target-blank-open-new-window.html
參考:
- https://spicyboyd.blogspot.com/2018/05/appandroid-webview-app.html
【APP/Android】如何使用 Webview 內嵌網頁,簡單的將網頁製作成 APP - SpicyBoyd 部落格 - https://ithelp.ithome.com.tw/articles/10262940
Day29 Android - 簡易內嵌網頁(webview) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 - https://stackoverflow.com/questions/5899087/android-webview-localstorage
html - Android webview & localStorage - Stack Overflow - https://blog.csdn.net/junjieking/article/details/7067855
match_parent和fill_parent的区别_junjieking的博客-CSDN博客_fill_parent - https://stackoverflow.com/questions/5761960/what-is-the-difference-between-match-parent-and-fill-parent
android - What is the difference between match_parent and fill_parent? - Stack Overflow - https://ithelp.ithome.com.tw/articles/10243182
Android Studio 菜鳥筆記本-Day 9-元件介紹-Layout(part 3) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 - https://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout
android - Set width to match constraints in ConstraintLayout - Stack Overflow - https://ithelp.ithome.com.tw/articles/10219479
[Android 十全大補] ConstraintLayout - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 - https://www.itread01.com/content/1546255105.html
Android:這是一份全面 & 詳細的Webview使用攻略 - 程式人生 - https://www.cnblogs.com/wushengwuxi/p/14044685.html
Android WebView基本用法简介 - 笪笠 - 博客园 - https://www.itread01.com/content/1546185063.html
android webview全面使用講解,以及一些遇到的坑 - 程式人生 - https://www.twblogs.net/a/5e4f0918bd9eee101e843d87
Android Webview知識點和遇到過的坑全總結 - 台部落 - https://tw511.com/a/01/13708.html
Android WebView使用常見問題以及解決方案(高階) - tw511教學網 - https://www.jianshu.com/p/3e6076c84e13
Android WebView 项目使用总结 - 簡書 - https://www.jianshu.com/p/2857d55e2f6e
WebChromeClient常用API与功能使用详解 - 簡書 - https://www.cnblogs.com/classloader/p/5302784.html
获取Android webview的点击元素 - 壮士暮年心不死 - 博客园 - https://www.twblogs.net/a/5bd39e072b717778ac2095db
Android Uri.parse的詳細資料 - 台部落
沒有留言:
張貼留言