在全屏 Webview Android 中设置进度条

3
我尝试使用这个解决方案Android WebView进度条,它可以为我提供以下功能:

    final ProgressBar Pbar;
    Pbar = (ProgressBar) findViewById(R.id.pB1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_browser);
    browser=(WebView)findViewById(R.id.layout_browser);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
    browser.getSettings().setPluginState(WebSettings.PluginState.ON);

    browser.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) 
           {
           if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
               Pbar.setVisibility(ProgressBar.VISIBLE);
           }
           Pbar.setProgress(progress);
           if(progress == 100) {
               Pbar.setVisibility(ProgressBar.GONE);
           }
        }
    });

    browser.loadUrl("google.com");

在我的Webview布局中添加进度条,但它会崩溃,为什么?
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ProgressBar android:id="@+id/pB1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true"
    android:padding="2dip">
</ProgressBar>
<WebView android:id="@+id/layout_browser" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/>   
</RelativeLayout>

注意:我将隐藏我的状态栏,因此我尝试使用这种方法。

1个回答

2

这行代码 Pbar = (ProgressBar) findViewById(R.id.pB1); 应该放在 setContentView(R.layout.layout_browser); 之后。

像这样:

setContentView(R.layout.layout_browser);
Pbar = (ProgressBar) findViewById(R.id.pB1);

好的,谢谢。现在它可以工作了,但是我在模拟器中看不到进度条。 - Lol Pallau
因为您的“ProgressBar”被隐藏在“WebView”后面。请先更改布局Xml文件中的代码,将“WebView”放在“ProgressBar”之前。 - user370305
如果我先加入webview,它会崩溃,你有什么想法吗? - Lol Pallau
好的,现在它可以工作了。我需要在我的代码中进行更改,但进度条出现在屏幕中央,我该如何将其上移? - Lol Pallau
1
在你的进度条中,将 android:layout_centerVertical="true" 改为 android:layout_alignParentTop="true" - user370305

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接