Android: The progress bar in the window's title does not display

前端 未结 6 1023
悲哀的现实
悲哀的现实 2020-12-07 15:38

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

This is the code:

    @Override
public v         


        
6条回答
  •  隐瞒了意图╮
    2020-12-07 16:36

    It appears that with Android 5.0 (API 21) and above Windows.FEATURE_PROGRESS no longer works. Taking some hints from the response by univasity, I was able to build a progress bar similar to those in other browsers.

    activity_main.xml"

    
    
    
    
    
    
    

    The FrameLayout allows the progress bar to float above the webview. Setting android:max to 100 makes it align with the default range in the activity, so the values don't have to be converted to the default of 0-10000 otherwise used by the progress bar. android:visibility="gone" makes the progress bar invisible by default.

    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/");
    }
    

提交回复
热议问题