【问题标题】:Xamarin Android WebView On page Navigation/Change Progress IndicatorXamarin Android WebView 页面导航/更改进度指示器
【发布时间】:2020-11-24 18:32:45
【问题描述】:

如何在 Xamarin Android 的 webview 中加载页面时显示进度指示器/栏

Webview webView = FindViewById(Resource.Id.webView);

        url = "https://www.examplemydemowebsiteaddress.com";
        //Enabled Javascript in Websettings  
        WebSettings websettings = webView.Settings;
        websettings.UseWideViewPort = true;
        websettings.DomStorageEnabled = true;
        websettings.JavaScriptEnabled = true;
        websettings.SetAppCacheEnabled(true);
        webView.SetWebViewClient(new WebViewClient());
        webView.LoadUrl(url);

【问题讨论】:

  • “我想要”不是问题。请阅读How to Ask 以获取有关编写主题问题的指导。
  • 请给我们一个代码,问我们一个正确的问题,并正确解释您想要做什么,以便我们帮助您解决您的问题。
  • 如何在 Xamarin Android 的 webview 中加载页面时显示进度指示器/栏?
  • 你需要在问题中添加一些相关代码。

标签: android xamarin webview preloader


【解决方案1】:

您需要在 WebView 上方定义一个 ProgressBar,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
  <ProgressBar
     android:id="@+id/pb"
     style="?android:attr/progressBarStyleHorizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:visibility="gone"        
    />
  <WebView
     android:id="@+id/webView"    
     android:layout_width="match_parent"
     android:layout_height="match_parent" /> 
</LinearLayout>

然后在你的活动中:

string url = "https://www.examplemydemowebsiteaddress.com";
ProgressBar prgBar = FindViewById<ProgressBar>(Resource.Id.pb);
webView = FindViewById<WebView>(Resource.Id.webView);
webView.SetWebChromeClient(new MyWebClient(prgBar));
webView.SetWebViewClient(new MyClient());
webView.LoadUrl(url );



class MyClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
        {
            view.LoadUrl(request.Url.ToString());
            return true;
        }
    }
class MyWebClient : WebChromeClient
    {
        ProgressBar prgBar;
        public MyWebClient(ProgressBar progressBar)
        {
            prgBar = progressBar;
        }
        public override void OnProgressChanged(WebView view, int newProgress)
        {
            if (newProgress == 100)
            {
                prgBar.Visibility= ViewStates.Gone;//The page progress bar disappears after loading 
            }
            else
            {
                prgBar.Visibility = ViewStates.Visible;//A progress bar is displayed when the web page starts loading
                prgBar.SetProgress(newProgress,false);//Set the progress value
            }
        }
    }

更新

在您的Resources/drawable 中创建一个progressbar.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item android:id="@android:id/background">
      <shape android:shape="rectangle">
        <solid android:color="#00ff00"></solid>
      </shape>
  </item>
  <item android:id="@android:id/progress">
      <clip>
        <shape android:shape="rectangle">
          <solid android:color="#ff0000"></solid>
        </shape>
      </clip>
   </item>

</layer-list>

然后在您的进度条

中添加android:progressDrawable="@drawable/progressbar"
<ProgressBar
   android:id="@+id/pb"
   style="?android:attr/progressBarStyleHorizontal"
   android:layout_width="match_parent"
   android:layout_height="3dp"
   android:progressDrawable="@drawable/progressbar"
   android:visibility="gone"        
/>

效果如下:

【讨论】:

  • No.... 在应用运行时,此代码在 Chrome 浏览器中打开页面
  • 你还需要webview.SetWebViewClient();
  • 看不懂,请相应修改以上代码..
  • @hsn 我这边一切正常,您可以尝试为进度条指示器设置颜色。检查上面的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 2021-11-12
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多