1. 程式人生 > >Xamarin(vs2015) Android控制元件 WebView載入網頁

Xamarin(vs2015) Android控制元件 WebView載入網頁

axml程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView1" />
</LinearLayout>


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<Android.Webkit.WebView>(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
    {
        /// <summary>
        /// 響應超連結事件,通過改變Load載入方式,
        /// 使在WebView控制元件中的連線在當前檢視中開啟,而不是新視窗中開啟
        /// </summary>
        /// <param name="view"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            //預設
            //return base.ShouldOverrideUrlLoading(view, url);
            view.LoadUrl(url);
            return true;
        }
    }