当前位置:  开发笔记 > 编程语言 > 正文

Android:窗口标题中的进度条不显示

如何解决《Android:窗口标题中的进度条不显示》经验,为你挑选了5个好方法。

我有一个Web视图来覆盖内置浏览器,我想在标题栏上显示进度指示器.

这是代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.browser);
    currentURL = BrowserActivity.this.getIntent().getExtras().getString("currentURL");

    try {
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new browserActivityClient());
        setProgressBarIndeterminateVisibility(true);
        mWebView.loadUrl(currentURL);
        setProgressBarIndeterminateVisibility(false);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Browser: " + e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    } 
}

我认为,根据我在网上看到的Android文档和其他示例,它应该可以工作.但它没有,你能告诉我我错在哪里吗?

还有一个问题:如果有时候我会选择android:theme="@android:style/Theme.NoTitleBar"在应用程序清单中声明,进度条是否会显示?

谢谢.



1> Manu..:

事实上,正确的代码是(测试和工作):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    currentURL = BrowserActivity.this.getIntent().getExtras().getString("currentURL");

    setContentView(R.layout.browser);

    setProgressBarIndeterminateVisibility(true);
    setProgressBarVisibility(true);

    try {
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new browserActivityClient());

        mWebView.setWebChromeClient(new WebChromeClient() {
           public void onProgressChanged(WebView view, int progress) {
               setProgress(progress * 100);
              if(progress == 100) {
                 setProgressBarIndeterminateVisibility(false);
                 setProgressBarVisibility(false);
              }
           }
        });
        mWebView.loadUrl(currentURL);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Browser: " + e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    } 
}



2> 小智..:

我也遇到了同样的问题.

因为我设置了android:theme ="@ android:style/Theme.NoTitleBar"

我通过在布局xml文件中添加ProgressBar来解决它,例如:






  


在代码中:

//加载网址时

progressBar.setProgress(0);
progressBar.setVisible(View.VISIBLE);

...

// loading ...进度已更改

progressBar.setProgress(progress);

...

//页面完成时

progressBar.setVisible(View.GONE);

完成!



3> 小智..:

我已经解决了这个问题.

您的电话setProgressBarVisibility()必须在onStart()onResume()之前,因为在此之前,您似乎无法保证可以更改可见性的任何视图.


对于Dralangus这个令人难以置信的解决方案,这里有一些经过全面测试的生产代码.非常感谢,Dralangus!最后,一个旋转器在设备旋转时不会消失.

public class MainActivity extends Activity
{
private static boolean showSpinner; // needs static

public void spinnerOn()
    {
    showSpinner = true;
    setProgressBarIndeterminateVisibility(true);
    }

public void spinnerOff()
    {
    showSpinner = false;
    setProgressBarIndeterminateVisibility(false);
    }

protected void onResume()
    {
    // solved by Dralangus http://stackoverflow.com/a/7414659/294884
    super.onResume();
    if (showSpinner)
        {
        spinnerOn();
        }
    else
        {
        spinnerOff();
        }
    }

protected void onStart()
    {
    super.onStart();
    // note, onResume is called EVEN LATER than onStart,
    // so your most reliable choice is onResume.
    // eternal thanks to Dralangus for this solution
    }

@Override
protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_main);

    ... // Your code
    }



4> Ivan Bartsov..:

看到一些答案如何混淆两个进度条,决定添加另一个答案

这些是针对顶部的水平进度条

requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarIndeterminate(true);
setProgress(intVal);
setProgressBarVisibility(true);

这些是针对动作栏中的微调器式进度条

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);

您不需要同时请求两个窗口功能,只需要您需要的进度类型.

OP代码中的错误是setProgressBarIndeterminateVisibility(true)没有请求的caling Window.FEATURE_INDETERMINATE_PROGRESS(反之亦然:调用setProgressBarIndeterminateVisibility()而不是setProgressBarVisibility())



5> Soren Stoutn..:

似乎Android 5.0(API 21)及以上版本Windows.FEATURE_PROGRESS不再适用.从一致性的反应中得到一些提示,我能够构建一个类似于其他浏览器的进度条.

activity_main.xml中"







FrameLayout允许浮在网页视图上方的进度条.设置android:max为100使其与活动中的默认范围对齐,因此不必将值转换为默认值0-10000,否则进度条将使用这些值. android:visibility="gone"默认情况下,进度条不可见.

MainActivty.java:

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

    // Get the WebView and the ProgressBar from activity_main.xml.
    final WebView mainWebView = (WebView) findViewById(R.id.webView);
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

    // Enables JavaScript.
    mainWebView.getSettings().setJavaScriptEnabled(true);

    // Updates the progress bar whenever there are changes and hides it again when it completes.
    mainWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            if (progress < 100) {
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.GONE);
                }
            progressBar.setProgress(progress);
        }
    });

    // Makes the mainWebView handle all URLs internally instead of calling the default browser.
    mainWebView.setWebViewClient(new WebViewClient());

    // Load a sample URL.
    mainWebView.loadUrl("http://developer.android.com/");
}

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有