没有URL地址栏的全屏Webview

时间:2016-02-19 17:11:51

标签: android webview android-webview

我们正在尝试将网站包装在移动应用程序中。该网站支持Chrome,IE9及以上版本和Safari。在尝试创建一个Android应用程序来包装网站时,下面是创建的布局

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

这是我的代码

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_web);
    new GcmRegistrationAsyncTask(this).execute();

    WebView myWebView = (WebView) findViewById(R.id.webview);

    Intent intent = getIntent();

    String url = intent.getStringExtra(Constants.URL);
    myWebView.setWebChromeClient(new WebChromeClient());
    //myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(url);
}

如果我只是使用WebViewClient,网站可以在全屏模式下正常工作,但由于它与Android Viewer不兼容,因此无法正常查看网站。如果我使用WebChromeClient,它会在单独的浏览器(Chrome)中正确打开网站,但会显示URL地址栏。我在同一个帖子上经历了很多帖子,但无法找出解决方案。这是我的最终目标

我需要在移动应用程序中打开网站,我想隐藏地址标题栏。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

我使用以下内容,它就像你想要的那样工作

WebView webView = (WebView) findViewById(R.id.webview);
ProgressBar progressBar = (ProgrssBar) findViewById(R.id.progressBar);  

String url = "https://www.google.com";

progressBar.setIndeterminate(false);
progressBar.setMax(100);

WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDefaultZoom(zoomDensity);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.requestFocus(View.FOCUS_DOWN);

webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(this.getWebViewClient());

progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);

WebChromeClientWebViewClient实现为;

WebChromeClient webChromeClient = new WebChromeClient() {

    @Override
    public void onProgressChanged(WebView view, int newProgress) {

        super.onProgressChanged(view, newProgress);
        progressBar.setProgress(newProgress);

    }
};

public WebViewClient getWebViewClient() {

    return new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            progressBar.setVisibility(View.VISIBLE);

            //Some checks
            if (url.contains("load.elsewhere.com")) {
                //If you want to handle where load.elsewhere.com is loaded, say in another external browser
                progressBar.setIndeterminate(true);

                //startActivity to load elsewhere

                return true;
            }

            return super.shouldOverrideUrlLoading(view, url);
        }
    };
}

我添加了ProgressBar以显示某些进展

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</FrameLayout>

答案 1 :(得分:0)

WebView默认情况下没有address bar其他内容必须将其转发到您的浏览器。 WebViews的API Guide显示此示例以覆盖shouldOverrideUrlLoading

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

这会将您的应用保留在您的应用中,而非现场链接可以由设备的浏览器处理。