java在加载webview应用程序时在顶部显示进度条
我试着在顶部显示一个进度条,像google chrome for 安卓这样的应用程序在加载网站时会显示加载条
我想在用户单击webview应用程序的任何内部链接时显示此进度条。。。就像谷歌chrome的节目一样
这是我尝试过的,这个代码不工作,应用程序停止工作,即使它没有启动
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:///安卓_asset/offline.html");
}
});
mWebView.loadUrl("http://www.example.com/?m=1");
}
}
主要活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
安卓:id="@+id/splash_screen"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_centerInParent="true"
安卓:src="@mipmap/splash_logo"
安卓:visibility="visible" />
<WebView
安卓:id="@+id/main_webview"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:visibility="gone" />
<ProgressBar
安卓:id="@+id/progressBar"
安卓:minHeight="2dip"
安卓:maxHeight="2dip"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
style="@安卓:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>
# 1 楼答案
你创建了MyWebChromeClient
在你的主要活动中
在活动区。xml
在drawable create bg_progress_bar_webview中。xml
希望!这对你有帮助