我正在制作一个简单的应用程序,其中我放了一个webview,但页面加载非常慢(尝试过很多网站),而且我想添加一个进度条,所以它应该显示该页面正在加载。 这是我用于webview的代码,但我不知道如何获得进度条:
public class MainActivity extends Activity {
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView1);
wv.setWebViewClient(new webCont());
wv.loadUrl(" http://www.abc.com" );
}
class webCont extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
答案 0 :(得分:3)
有进度条。显示相同。您可以自定义以下内容以满足您的需求。要加载速度更快,取决于连接速度。
覆盖onPageFinished
并将可见性设置为已在webcount
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.wv);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://slashdot.org/");
}
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
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
//super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
else
{
finish();
return true;
}
}
}
XML
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:id="@+id/wv"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>
扣
不滚动就看不到整个页面
向右滚动
答案 1 :(得分:3)
在WebChromeClient()
上设置网页浏览并尝试使用此代码
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
答案 2 :(得分:1)
public class MainActivity extends Activity {
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl(" http://www.abc.com" );
//Show Progress layout
showProgressLayout();
wv.setWebViewClient(new webCont());
}
class webCont extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//Hide Progress bar.
hideProgressLayout();
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
//If any error comes this method will handle for webview.
hideProgressLayout();
if (view.canGoBack()) {
view.goBack();
}
You can write your own Dialog or Toast message if you want to show error message
}
}