我正在制作一个简单的应用程序,其中我放了一个webvIEw,但页面加载非常慢(尝试过很多网站),而且我想添加一个进度条,所以它应该显示该页面正在加载.
这是我用于webvIEw的代码,但我不知道如何获得进度条:
public class MainActivity extends Activity {WebVIEw wv;@OverrIDeprotected 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>
捕捉
没有滚动,你看不到整个页面
滚动正确
总结以上是内存溢出为你收集整理的android – 如何使WebView加载快速并为其添加进度条全部内容,希望文章能够帮你解决android – 如何使WebView加载快速并为其添加进度条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)