# android webview app 筆記 ## 環境 - Android Studio 4.1.1 - gradle:4.0.0 - kotlin_version = "1.3.72" - compileSdkVersion 30 - buildToolsVersion "30.0.2" ## MainActivity ```kotlin= package com.xxx.xxx import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.webkit.WebView; import android.webkit.WebViewClient; class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val webView = findViewById<WebView>(R.id.webview) val webSettings = webView.settings webSettings.javaScriptEnabled = true webSettings.domStorageEnabled = true webSettings.allowFileAccess = true webSettings.allowContentAccess = true setContentView(webView) webView.webViewClient = WebViewClient() webView.loadUrl("https://www.google.com/") } } ``` ## 各項設定 ### LocalStorage ```kotlin= val webSettings = webView.settings webSettings.javaScriptEnabled = true webSettings.domStorageEnabled = true webSettings.allowFileAccess = true webSettings.allowContentAccess = true ``` --- ### 全螢幕(不顯示 status bar) 修改 `res\values\styles.xml` 1. 關閉 action bar: `<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">` 2. 關閉 status bar: `<item name="android:windowFullscreen">true</item>` #### 結果 ``` <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowFullscreen">true</item> </style> </resources> ``` --- ### 強制水平顯示 - [android:screenOrientation](https://developer.android.com/guide/topics/manifest/activity-element#screen) 加在 `MainActivity` 的節點屬性上 ```xml= <manifest> <application> <activity android:name=".MainActivity" android:screenOrientation="landscape"> ``` --- ### 設定 output APK 名稱 ``` android.applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFileName = new File(defaultConfig.applicationId + "-" + buildType.name + "-v" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk"); } } ``` ## 參考 - [使用 Android Webview 建立 App](http://heavenchou.buddhason.org/node/333) - [Android Webview H5交互之LocalStorage](https://www.jianshu.com/p/379a0681ce25) - [Android webview slow](https://stackoverflow.com/questions/7422427/android-webview-slow) - [android.view.View.systemUiVisibility deprecated. What is the replacement?](https://stackoverflow.com/a/62578405/6573523) - [[Android] 隱藏狀態列、標題列 (hide action bar and status bar)](http://dog0416.blogspot.com/2018/04/android-hide-action-bar-and-status-bar.html) - [Android Studio apk打包自定义包名](https://blog.csdn.net/github_37472200/article/details/78537592) - [Create app icons with Image Asset Studio](https://developer.android.com/studio/write/image-asset-studio)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up