Java 加载webview应用程序时在顶部显示进度条
我试着在顶部显示一个进度条,像google chrome for android这样的应用程序在加载网站时会显示加载条 我想在用户单击webview应用程序的任何内部链接时显示此进度条…就像google chrome显示的那样 这是我尝试过的,这个代码不工作,应用程序停止工作,即使它没有启动Java 加载webview应用程序时在顶部显示进度条,java,android,webview,Java,Android,Webview,我试着在顶部显示一个进度条,像google chrome for android这样的应用程序在加载网站时会显示加载条 我想在用户单击webview应用程序的任何内部链接时显示此进度条…就像google chrome显示的那样 这是我尝试过的,这个代码不工作,应用程序停止工作,即使它没有启动 public class MainActivity extends AppCompatActivity { private WebView mWebView; private Progre
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.main_webview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
if (mWebView.getProgress() == 100) {
// show webview
mWebView.setVisibility(View.VISIBLE);
// hide splash
findViewById(R.id.splash_screen).setVisibility(View.GONE);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
view.loadUrl("file:///android_asset/offline.html");
}
});
mWebView.loadUrl("http://www.example.com/?m=1");
}
}
主要活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/splash_logo"
android:visibility="visible" />
<WebView
android:id="@+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>
您可以创建MyWebChromeClient
public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;
public MyWebChromeClient(ProgressListener listener) {
mListener = listener;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
mListener.onUpdateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
public interface ProgressListener {
public void onUpdateProgress(int progressValue);
}
}
在你的主要活动中
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private WebView mWebView;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// add progress bar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebChromeClient(new MyWebChromeClient(this));
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
});
}
@Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}
在activity_main.xml中
<RelativeLayout
android:id="@+id/relative_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="@dimen/progress_bar_height"
android:progressDrawable="@drawable/bg_progress_bar_webview" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar" />
</RelativeLayout>
在drawable中创建bg_progress_bar_webview.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"
android:drawable="@android:color/transparent"/>
<item android:id="@android:id/secondaryProgress">
<scale
android:drawable="@color/progress_bar_second"
android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
<scale
android:drawable="@color/progress_bar_runing"
android:scaleWidth="100%" />
</item>
</layer-list>
希望!它可以帮助您创建MyWebChromeClient
public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;
public MyWebChromeClient(ProgressListener listener) {
mListener = listener;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
mListener.onUpdateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
public interface ProgressListener {
public void onUpdateProgress(int progressValue);
}
}
在你的主要活动中
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private WebView mWebView;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// add progress bar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebChromeClient(new MyWebChromeClient(this));
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
});
}
@Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}
在activity_main.xml中
<RelativeLayout
android:id="@+id/relative_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="@dimen/progress_bar_height"
android:progressDrawable="@drawable/bg_progress_bar_webview" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar" />
</RelativeLayout>
在drawable中创建bg_progress_bar_webview.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"
android:drawable="@android:color/transparent"/>
<item android:id="@android:id/secondaryProgress">
<scale
android:drawable="@color/progress_bar_second"
android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
<scale
android:drawable="@color/progress_bar_runing"
android:scaleWidth="100%" />
</item>
</layer-list>
希望!它帮助您在调用
setContentView()
之后将findViewById()调用移动到。除此之外,在WebView
上设置WebChromeClient
,以在其onProgressChanged()
方法中获取页面进度更新。将findViewById()
调用移动到setContentView()
调用之后。除此之外,还可以在WebView
上设置WebChromeClient
,以在其onProgressChanged()
方法中获取页面进度更新。谢谢。。它的工作,但一个问题是,当进度条显示,该网站得到的利润在顶部。。。我想在页面上显示,而不是在页面上显示。。。希望你能理解我的意思,我希望它处于绝对位置。你可以看到我的activity\u main.xml!!!删除这一行对我来说很有用:D非常感谢。OnPageStarted和OnPageFinished都是无用的:拒绝在以前失败的类java.lang.ClassThank上重新初始化。。它的工作,但一个问题是,当进度条显示,该网站得到的利润在顶部。。。我想在页面上显示,而不是在页面上显示。。。希望你能理解我的意思,我希望它处于绝对位置。你可以看到我的activity\u main.xml!!!删除这一行对我来说很有用:D非常感谢。OnPageStarted和OnPageFinished都是无用的:拒绝在以前失败的类java.lang.class上重新初始化