gpt4 book ai didi

android - 如何在tab2 webView中添加进度条

转载 作者:太空狗 更新时间:2023-10-29 13:17:25 26 4
gpt4 key购买 nike

我想在 webView 中添加进度,但是当我尝试按照 this 添加进度条时和 this但这太令人困惑了,我无法理解我是新手。

我想在我的 tab2 Activity 中添加一个进度条

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;



}

这是我的 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>

最佳答案

试试这个方法,

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);
}
}


}

关于android - 如何在tab2 webView中添加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33436752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com