Android的 - web视图进度 [英] Android - Webview Progressbar

查看:190
本文介绍了Android的 - web视图进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加web视图上方的进度条。 每当我点击一个链接,我刚才提出的进度可见。

I have added the progressbar above the webview. Whenever the i click a link, I just made the progressbar to visible.

我想在web视图的进度覆盖,我想表现的进度栏的百分比。我知道的CSS,但我不知道如何改变在Android的进度位置

I want to have the progressbar overlay on the webview and i want to show the percentage of progress bar. I know css, but i don't know how to change the position of progressbar in android

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/> 

MainActivity.java

MainActivity.java

public class MainActivity extends Activity {

WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    web = (WebView) findViewById(R.id.webview01);
 // request the progress-bar feature for the activity
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    // set a webChromeClient to track progress    
    web.setWebChromeClient(new WebChromeClient()
    {
        public void onProgressChanged(WebView view, int progress)
        {
            // update the progressBar
            MainActivity.this.setProgress(progress * 100);
        }
    });


    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("url");

}

// 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;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

要显示进度百分比,是否需要修改此code?或者,我可以从这个code扩展功能?

To display progressbar percentage, do i need to modify this code? or can i extend the functionality from this code?

推荐答案

要显示页面加载进度条,使用这样的:

To show a "page load" progress bar, use this:

在的onCreate():

in onCreate():

// request the progress-bar feature for the activity
getWindow().requestFeature(Window.FEATURE_PROGRESS);

// set a webChromeClient to track progress    
myWebView.setWebChromeClient(new WebChromeClient()
{
 public void onProgressChanged(WebView view, int progress)
 {
   // update the progressBar
   MyActivity.this.setProgress(progress * 100);
 }
});

这将显示使用内置的造型在屏幕的顶部,就像内置的浏览器。

This will show using built-in styling at the top of the screen, just like the built-in browser.

相反,如果你想显示和更新自己的进度,这个过程是相似的,得到一个处理进度条:

If instead you want to display and update your own ProgressBar, the process is similar, get a handle to your progress bar:

ProgressBar myProgress = (ProgressBar)findViewById(R.id.progressBar1);

。 。 。 而在onProgressChanged()函数,使用:

. . . and in the onProgressChanged() function, use this:

myProgress.setProgress(progress * 100);

而不是MyActivity.this.setProgress

()。

instead of MyActivity.this.setProgress().

如果你想有进度条出现在web视图前进一步,不能使用的LinearLayout。使用带有centerInParent =真正的一个RelativeLayout的,并在布局中的WebView后列出进度。

Further, can't use a LinearLayout if you want to have ProgressBar appear in front of the WebView. Use a RelativeLayout with centerInParent="true", and list the ProgressBar after the WebView in your layout.

这篇关于Android的 - web视图进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆