当前位置:网站首页>短视频软件开发,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,更多内容欢迎关注之后的文章
边栏推荐
- 如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
- To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
- When compiling a program with boost library with VS2013, it prompts fatal error C1001: An internal error occurred in the compiler
- 【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
- explain each field introduction
- Hardware Bear Original Collection (Updated 2022/07)
- SQL函数 TO_DATE(二)
- 硬件大熊原创合集(2022/07更新)
- 用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
- GZIPOutputStream 类源码分析
猜你喜欢

How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial

MySQL中超键、主键及候选键的区别是什么

从普通进阶成优秀的测试/开发程序员,一路过关斩将

DAO开发教程【WEB3.0】

力扣刷题之移动零

深入浅出Flask PIN

XML配置

Win11怎么安装语音包?Win11语音包安装教程

What should I do if the Win11 campus network cannot be connected?Win11 can't connect to campus network solution

暑假第二周总结博客
随机推荐
What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法
[pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio
SQL function TO_DATE (2)
【LeetCode】Day109-the longest palindrome string
Choosing the right DevOps tool starts with understanding DevOps
Win11如何删除升级包?Win11删除升级包的方法
C#/VB.NET: extracted from the PDF document all form
GZIPOutputStream 类源码分析
在表格数据上,为什么基于树的模型仍然优于深度学习?
How many steps does it take to convert an ENS domain name into music?
想随时、随地、随心使用数据库的朋友们,全体注意!
通配符 SSL/TLS 证书
The life cycle and scope
#yyds dry goods inventory# Interview must brush TOP101: the last k nodes in the linked list
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
Industry Salon Phase II丨How to enable chemical companies to reduce costs and increase efficiency through supply chain digital business collaboration?
LeetCode 0151.颠倒字符串中的单词
odoo 编码规范(编程规范、编码指南)