我正在制作一个简单的应用程序,其中我放了一个webview,但页面加载非常慢(尝试过很多网站),而且我想添加一个进度条,所以它应该显示该页面正在加载. 这是我用于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; } }有一个进度条.显示相同.您可以自定义以下内容以满足您的需求.要加载速度更快,取决于连接速度.
覆盖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>
捕捉
没有滚动,你看不到整个页面
滚动正确