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

Xamarin(vs2015) Android控件 WebView加载网页_第1张图片


axml代码:



    


Activity代码:

    [Activity(Label = "App6", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;
        Android.Webkit.WebView webview;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //隐藏标题栏(须放在SetContentView函数之前)
            this.RequestWindowFeature(WindowFeatures.NoTitle);
            //设置全屏
            this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            //需引用using Android.Webkit;命名空间
            //使用默认的浏览器打开网页
            // Android.Webkit.WebView webview = new Android.Webkit.WebView(this);
            // webview.LoadUrl("http://www.baidu.com");

            //使用WebView控件打开指定网页
            Android.Webkit.WebView webview2 = FindViewById(Resource.Id.webView1);
            this.webview = webview2;
              webview2.LoadUrl("http://www.5525.org/");
            //加载项目中本地文件夹Assets下的test.html文件
            // webview2.LoadUrl("file:///android_asset/test.html");
            // webview2.LoadUrl("file:///android_asset/abc/test.html");
            //启用脚本
            webview2.Settings.JavaScriptEnabled = true;

            #region --缩放--
            //设置支持缩放(前提是网页自身支持缩放)
            webview2.Settings.SetSupportZoom(true);
            webview2.Settings.BuiltInZoomControls = true;

            //支持任意比例缩放
            webview2.Settings.UseWideViewPort = true;
            //显示缩放控件(放大/缩小按钮)
            webview2.Settings.DisplayZoomControls = false;

            //自适应屏幕
            //  webview2.Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn);
            //webview2.Settings.LoadWithOverviewMode = true;
            #endregion

            //webview2.ClearCache(true);
            //后退
            //webview2.GoBack();
            webview2.SetWebViewClient(new ExtWebViewClient());

        }
        public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            //当按下后退键时,返回WebView上一页面,而不是结束
            if (keyCode == Keycode.Back && webview.CanGoBack())
            {
                webview.GoBack();
                return true;
            }
            //else
            //{
            //    return false;
            //}
            //默认
            return base.OnKeyDown(keyCode, e);
        }
    }
    public class ExtWebViewClient : Android.Webkit.WebViewClient
    {
        /// 
        /// 响应超链接事件,通过改变Load加载方式,
        /// 使在WebView控件中的连接在当前视图中打开,而不是新窗口中打开
        /// 
        /// 
        /// 
        /// 
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            //默认
            //return base.ShouldOverrideUrlLoading(view, url);
            view.LoadUrl(url);
            return true;
        }
    }


你可能感兴趣的:(Xamarin(vs2015) Android控件 WebView加载网页)