简体   繁体   English

如何在tab2 webView中添加进度条

[英]how to add progress bar in tab2 webView

i want to add a progress in a webView but when i tried to add progress bar by following this and this but it's so confusing i can't undersatnd i am a newbie. 我想在webView中添加一个进度但是当我试图通过跟随这个这个来添加进度条但它是如此令人困惑我不能低估我是一个新手。

i want to add a progress bar in my tab2 activity 我想在tab2活动中添加进度条

package com.freerechargeapp.weebly.free_recharge_app;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;


public class tab2 extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tab2, container, false);





    WebView webView = (WebView) view.findViewById(R.id.webview1);
    webView.loadUrl("http://google.com");



    return view;



}

} }

here is my tab2.xml 这是我的tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"


 />

    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/progressBar1"/>




</RelativeLayout>

</LinearLayout>

Try this way, 试试这个,

public class tab2 extends Fragment {

WebView webView ;
 ProgressBar progressBar;

  @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab2, container, false);



        webView = (WebView) view.findViewById(R.id.webview1);
        progressBar = (ProgressBar)view.findViewById(R.id.progressBar1);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://google.com");

        return view;
  }

public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
                progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM