当前位置:网站首页>短视频软件开发,Android开发,使用Kotlin实现WebView
短视频软件开发,Android开发,使用Kotlin实现WebView
2022-08-01 19:01:00 【云豹网络科技】
短视频软件开发,Android开发,使用Kotlin实现WebView
1、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2、Activity文件
class TestWebActivity : Activity(){
private var webView: WebView? = null
var WEB_URL = "https://www.baidu.com"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.test_webview)
initWebView()
}
fun initWebView(){
webView = findViewById(R.id.webview)
webView?.loadUrl(WEB_URL)
val webClient = object : WebViewClient(){
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return false
}
}
//下面这些直接复制就好
webView?.webViewClient=webClient
var webSettings = webView!!.settings
webSettings.javaScriptEnabled = true // 开启 JavaScript 交互
webSettings.setAppCacheEnabled(true) // 启用或禁用缓存
webSettings.cacheMode = WebSettings.LOAD_DEFAULT // 只要缓存可用就加载缓存, 哪怕已经过期失效 如果缓存不可用就从网络上加载数据
webSettings.setAppCachePath(cacheDir.path) // 设置应用缓存路径
// 缩放操作
webSettings.setSupportZoom(false) // 支持缩放 默认为true 是下面那个的前提
webSettings.builtInZoomControls = false // 设置内置的缩放控件 若为false 则该WebView不可缩放
webSettings.displayZoomControls = false // 隐藏原生的缩放控件
webSettings.blockNetworkImage = false // 禁止或允许WebView从网络上加载图片
webSettings.loadsImagesAutomatically = true // 支持自动加载图片
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
webSettings.safeBrowsingEnabled = true // 是否开启安全模式
}
webSettings.javaScriptCanOpenWindowsAutomatically = true // 支持通过JS打开新窗口
webSettings.domStorageEnabled = true // 启用或禁用DOM缓存
webSettings.setSupportMultipleWindows(true) // 设置WebView是否支持多窗口
// 设置自适应屏幕, 两者合用
webSettings.useWideViewPort = true // 将图片调整到适合webview的大小
webSettings.loadWithOverviewMode = true // 缩放至屏幕的大小
webSettings.allowFileAccess = true // 设置可以访问文件
webSettings.setGeolocationEnabled(true) // 是否使用地理位置
webView?.fitsSystemWindows = true
webView?.setLayerType(View.LAYER_TYPE_HARDWARE,null)
webView?.loadUrl(WEB_URL)
}
//设置返回键的监听
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode==KeyEvent.KEYCODE_BACK){
if (webView!!.canGoBack()){
webView!!.goBack() //返回上一个页面
return true
}else{
finish()
return true
}
}
return false
}
}
3、在AndroidMinifest中添加权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.testwebview">
<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/AppTheme">
<activity android:name=".TestWebActivity">
<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"/>
</manifest>
以上就是 短视频软件开发,Android开发,使用Kotlin实现WebView,更多内容欢迎关注之后的文章
边栏推荐
- modbus总线模块DAM-8082
- 有点奇怪!访问目的网址,主机能容器却不行
- Screen: GFF, OGS, Oncell, Incell of full lamination process
- 升哲科技携全域数字化方案亮相2022全球数字经济大会
- 483-82(23、239、450、113)
- explain 各字段介绍
- MySQL数据库————流程控制
- MLX90640 Infrared Thermal Imager Temperature Measurement Module Development Notes (Complete)
- 使用常见问题解答软件的好处有哪些?
- 用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
猜你喜欢

log factory (detail)

Keras深度学习实战——交通标志识别

#yyds干货盘点# 面试必刷TOP101: 链表中倒数最后k个结点

力扣刷题之合并两个有序数组

MySQL database - stored procedures and functions

Win11如何删除升级包?Win11删除升级包的方法

三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.

Shell script topic (07): file from cfs to bos
![[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)](/img/8d/4f5f7c2463b781cba1c68370d3c29c.png)
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)

日志工厂(详细)
随机推荐
小白系统初始化配置资源失败怎么办
123123123123
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
No need to crack, install Visual Studio 2013 Community Edition on the official website
TestNG多个xml进行自动化测试
MySQL数据库————存储过程和函数
【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
TestNG multiple xml for automated testing
Keras深度学习实战——交通标志识别
C#/VB.NET 从PDF中提取表格
Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
Hardware Bear Original Collection (Updated 2022/07)
基于flowable的upp(统一流程平台)运行性能优化
硬件大熊原创合集(2022/07更新)
The XML configuration
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
modbus bus module DAM-8082
行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
英国伦敦大学|眼科强化学习:潜在应用和实施挑战
How to record and analyze your alchemy process - use notes of the visual artifact Wandb [1]