public static void AttachedWebView(string webViewKey, RelativeLayout rl) { if (WebViewAssets.ContainsKey(webViewKey)) { if (RelativeLayoutWebViewKey == null) { RelativeLayoutWebViewKey = new Dictionary <string, RelativeLayout>(); } if (RelativeLayoutWebViewKey.ContainsKey(webViewKey)) { RelativeLayoutWebViewKey[webViewKey].RemoveView(WebViewAssets[webViewKey]); WebViewAssets[webViewKey].Visibility = ViewStates.Invisible; RelativeLayoutWebViewKey.Remove(webViewKey); } else { rl.AddView(WebViewAssets[webViewKey]); RelativeLayoutWebViewKey.Add(webViewKey, rl); Task.Run(() => { Thread.Sleep(600); MainActivity.thisActivity.RunOnUiThread((() => { X5WebViewHelper.WebViewAssets[webViewKey].Visibility = ViewStates.Visible; })); }); } } }
public static string WebViewInit(string pageId) { if (WebViewAssets == null) { WebViewAssets = new Dictionary <string, X5WebViewHelper>(); } if (AnalyticAgreement.PageParam[pageId].IsRelative && WebViewAssets.ContainsKey(AnalyticAgreement.PageParam[pageId].ParentWebViewKey)) { AnalyticAgreement.PageParam[pageId].WebViewKey = AnalyticAgreement.PageParam[pageId].ParentWebViewKey; AnalyticAgreement.PageParam[pageId].IsLoadFinished = AnalyticAgreement.PageParam[AnalyticAgreement.PageParam[pageId].ParentPageId].IsLoadFinished; return(AnalyticAgreement.PageParam[pageId].WebViewKey); } if (string.IsNullOrWhiteSpace(AnalyticAgreement.PageParam[pageId].WebViewKey) || !WebViewAssets.ContainsKey(AnalyticAgreement.PageParam[pageId].WebViewKey)) { var x5WebView = new X5WebViewHelper(MainActivity.thisActivity.Application.ApplicationContext); if (string.IsNullOrWhiteSpace(AnalyticAgreement.PageParam[pageId].WebViewKey)) { AnalyticAgreement.PageParam[pageId].WebViewKey = GetRandomString(); } WebViewAssets.Add(AnalyticAgreement.PageParam[pageId].WebViewKey, x5WebView); } WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].ScrollBarStyle = ScrollbarStyles.InsideOverlay; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].VerticalScrollBarEnabled = false; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].HorizontalScrollBarEnabled = false; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.JavaScriptEnabled = true; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.UseWideViewPort = true; //缓存模式 WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.DomStorageEnabled = true; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetAppCacheMaxSize(1024 * 1024 * 8);//设置缓冲大小,我设的是8M string appCacheDir = MainActivity.thisActivity.GetDir("appcache", FileCreationMode.Private).Path; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetAppCachePath(appCacheDir); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetAppCacheEnabled(true); string appDatabaseDir = MainActivity.thisActivity.GetDir("databases", FileCreationMode.Private).Path; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.DatabasePath = appDatabaseDir; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.DatabaseEnabled = true; string appGeolocationDir = MainActivity.thisActivity.GetDir("geolocation", FileCreationMode.Private).Path; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetGeolocationDatabasePath(appGeolocationDir); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetGeolocationEnabled(true); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.AllowContentAccess = true; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.AllowFileAccess = true; //网页自适应 WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.LoadWithOverviewMode = true; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.DefaultTextEncodingName = "utf-8"; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].SetWebViewClient(new ExtWebViewClient()); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].SetWebChromeClient(new ExtWebChromeClient()); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.NarrowColumns); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetSupportZoom(false); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].Settings.SetSupportMultipleWindows(false); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].SetDownloadListener(new DownloadListener()); WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].SetOnLongClickListener(WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey]); var hostUrl = HybridCommon.Config.HtmlServerHost + "/index.html#/Empty/" + pageId; //var hostUrl = "file:///" + DeviceInfo.HtmlFolder + "/index.html#/Empty/" + pageId; WebViewAssets[AnalyticAgreement.PageParam[pageId].WebViewKey].LoadUrl(hostUrl); CookieSyncManager.CreateInstance(MainActivity.thisActivity.Application.ApplicationContext); CookieSyncManager.Instance.Sync(); return(AnalyticAgreement.PageParam[pageId].WebViewKey); }