Xamarin(vs2015) Android控件 WebView加载网页

来源:互联网 发布:paxos算法细节详解 编辑:程序博客网 时间:2024/06/09 16:37

axml代码:

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:minWidth="25px"  
  7.     android:minHeight="25px">  
  8.     <WebView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:id="@+id/webView1" />  
  12. </LinearLayout>  


Activity代码:

[csharp] view plain copy
print?
  1. [Activity(Label = "App6", MainLauncher = true, Icon = "@drawable/icon")]  
  2. public class MainActivity : Activity  
  3. {  
  4.     int count = 1;  
  5.     Android.Webkit.WebView webview;  
  6.     protected override void OnCreate(Bundle bundle)  
  7.     {  
  8.         base.OnCreate(bundle);  
  9.         //隐藏标题栏(须放在SetContentView函数之前)  
  10.         this.RequestWindowFeature(WindowFeatures.NoTitle);  
  11.         //设置全屏  
  12.         this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);  
  13.         // Set our view from the "main" layout resource  
  14.         SetContentView(Resource.Layout.Main);  
  15.   
  16.         //需引用using Android.Webkit;命名空间  
  17.         //使用默认的浏览器打开网页  
  18.         // Android.Webkit.WebView webview = new Android.Webkit.WebView(this);  
  19.         // webview.LoadUrl("http://www.baidu.com");  
  20.   
  21.         //使用WebView控件打开指定网页  
  22.         Android.Webkit.WebView webview2 = FindViewById<Android.Webkit.WebView>(Resource.Id.webView1);  
  23.         this.webview = webview2;  
  24.           webview2.LoadUrl("http://www.5525.org/");  
  25.         //加载项目中本地文件夹Assets下的test.html文件  
  26.         // webview2.LoadUrl("file:///android_asset/test.html");  
  27.         // webview2.LoadUrl("file:///android_asset/abc/test.html");  
  28.         //启用脚本  
  29.         webview2.Settings.JavaScriptEnabled = true;  
  30.  
  31.         #region --缩放--  
  32.         //设置支持缩放(前提是网页自身支持缩放)  
  33.         webview2.Settings.SetSupportZoom(true);  
  34.         webview2.Settings.BuiltInZoomControls = true;  
  35.   
  36.         //支持任意比例缩放  
  37.         webview2.Settings.UseWideViewPort = true;  
  38.         //显示缩放控件(放大/缩小按钮)  
  39.         webview2.Settings.DisplayZoomControls = false;  
  40.   
  41.         //自适应屏幕  
  42.         //  webview2.Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn);  
  43.         //webview2.Settings.LoadWithOverviewMode = true;  
  44.         #endregion  
  45.   
  46.         //webview2.ClearCache(true);  
  47.         //后退  
  48.         //webview2.GoBack();  
  49.         webview2.SetWebViewClient(new ExtWebViewClient());  
  50.   
  51.     }  
  52.     public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)  
  53.     {  
  54.         //当按下后退键时,返回WebView上一页面,而不是结束  
  55.         if (keyCode == Keycode.Back && webview.CanGoBack())  
  56.         {  
  57.             webview.GoBack();  
  58.             return true;  
  59.         }  
  60.         //else  
  61.         //{  
  62.         //    return false;  
  63.         //}  
  64.         //默认  
  65.         return base.OnKeyDown(keyCode, e);  
  66.     }  
  67. }  
  68. public class ExtWebViewClient : Android.Webkit.WebViewClient  
  69. {  
  70.     /// <summary>  
  71.     /// 响应超链接事件,通过改变Load加载方式,  
  72.     /// 使在WebView控件中的连接在当前视图中打开,而不是新窗口中打开  
  73.     /// </summary>  
  74.     /// <param name="view"></param>  
  75.     /// <param name="url"></param>  
  76.     /// <returns></returns>  
  77.     public override bool ShouldOverrideUrlLoading(WebView view, string url)  
  78.     {  
  79.         //默认  
  80.         //return base.ShouldOverrideUrlLoading(view, url);  
  81.         view.LoadUrl(url);  
  82.         return true;  
  83.     }  
  84. }