用到了腾讯x5的webview 当然原生的也可以
//腾讯x5 implementation 'com.tencent.tbs.tbssdk:sdk:43939' MyApplication->onCreate新增 我的是kt语法,,具体就是创建map添加进去
// 在调用TBS初始化、创建WebView之前进行如下配置 // 在调用TBS初始化、创建WebView之前进行如下配置 val map = mapOf(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER to true,TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE to true) QbSdk.initTbsSettings(map)自定义webview
package com.anguomob.video.view import android.annotation.SuppressLint import android.content.Context import android.util.AttributeSet import com.tencent.smtt.sdk.WebSettings import com.tencent.smtt.sdk.WebView import com.tencent.smtt.sdk.WebViewClient class X5WebView @SuppressLint("SetJavaScriptEnabled") constructor( arg0: Context?, arg1: AttributeSet? ) : WebView(arg0, arg1) { private val client: WebViewClient = object : WebViewClient() { /** * 防止加载网页时调起系统浏览器 */ override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean { view.loadUrl(url) return true } } private fun initWebViewSettings() { val webSetting: WebSettings = this.getSettings() webSetting.setJavaScriptEnabled(true) webSetting.setJavaScriptCanOpenWindowsAutomatically(true) webSetting.setAllowFileAccess(true) webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS) webSetting.setSupportZoom(true) webSetting.setBuiltInZoomControls(true) webSetting.setUseWideViewPort(true) webSetting.setSupportMultipleWindows(true) // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true) // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true) webSetting.setGeolocationEnabled(true) webSetting.setAppCacheMaxSize(Long.MAX_VALUE) // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND) // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE) // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension // settings 的设计 } init { this.setWebViewClient(client) // this.setWebChromeClient(chromeClient); // WebStorage webStorage = WebStorage.getInstance(); initWebViewSettings() this.getView().setClickable(true) } }总结
核心是因为下面的client没有设置 所以才跳转到 了系统默认浏览器 可以直接用webweb.setVewViewClient去设置一下就可以了
private val client: WebViewClient = object : WebViewClient() { /** * 防止加载网页时调起系统浏览器 */ override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean { view.loadUrl(url) return true } } this.setWebViewClient(client)使用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <com.anguomob.video.view.X5WebView android:id="@+id/forum_context" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="5dp" android:paddingRight="5dp" /> </LinearLayout> package com.anguomob.video.activity import android.os.Bundle import android.view.KeyEvent import android.view.View import androidx.appcompat.widget.Toolbar import com.anguomob.video.R import com.tencent.smtt.sdk.WebSettings import com.tencent.smtt.sdk.WebView import com.tencent.smtt.sdk.WebViewClient import com.yilan.sdk.common.ui.BaseActivity /** * Create by: liuan * Create date: 2020-10-19 0019 * Describe: */ class WebViewX5Acitivity : BaseActivity() { lateinit var mWebView: WebView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_x5_webview) mWebView = findViewById<WebView>(R.id.forum_context); mWebView.loadUrl("https://www.anguomob.com") } // 5、覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法 override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { return if (keyCode == KeyEvent.KEYCODE_BACK) { if (mWebView.canGoBack()) { mWebView.goBack() //goBack()表示返回WebView的上一页面 true } else { finish() true } } else false } }