安卓:在窗口的标题不显示进度条进度条、窗口、标题

2023-09-12 23:42:26 作者:暗藏城府

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

I have a web view to override the built-in browser and I want to show a progress indicator on the title bar.

这是在code:

    @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的文档,我在网上看到其他样品。但它没有,请你告诉我,我错了?

It should work, I think, according to Android docs and other samples I saw on the net. But it doesn't, could you please tell me where am I wrong?

和另外一个问题:如果有时后我会选择申报安卓主题=@安卓风格/ Theme.NoTitleBar在应用程序清单,将在进度条显示了没有?

And another question: if sometimes later I'll choose to declare android:theme="@android:style/Theme.NoTitleBar" in the application manifest, will the progress bar show anymore or not?

感谢你。

推荐答案

其实正确的code为(测试工作):

In fact the correct code is (tested and working):

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();
    } 
}