繁体   English   中英

XML启动画面 - >显示Webview

[英]XML Splash Screen -> Display Webview

谢谢你试图解决我的问题!

目前我的应用程序基本上使用Android Web视图加载网页。 缺点是有时(连接不良)网页需要10秒以上才能加载。 有没有办法在网页加载时创建某种启动XML“加载”屏幕?

看看我的代码......

package com.ect;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class SomeActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //THIS IS WHAT I WANT: setContentView(R.layout.main);

        CookieSyncManager.createInstance(getBaseContext());

        // Let's display the progress in the activity title bar, like the
        // browser app does.


        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        WebView webview = new WebView(this);
        setContentView(webview);


        webview.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
             // Activities and WebViews measure progress with different scales.
             // The progress meter will automatically disappear when we reach 100%
             activity.setProgress(progress * 1000);
        }
      });

webview.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Users will be notified in case there's an error (i.e. no internet connection)
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}

public void onPageFinished(WebView view, String url) {
    CookieSyncManager.getInstance().sync();
}
});

CookieSyncManager.getInstance().startSync();
CookieSyncManager.getInstance().sync();

     //This will load the webpage that we want to see
      webview.loadUrl("http://www.web.com/");

   }
}

那是我的代码。 如果仔细观察,我发表评论说:“这就是我想要的:......”

该注释将调用main.xml文件,但如果我使用该注释,将显示main.xml文件,但webview永远不会出现...

如何显示main.xml文件然后(一旦加载Web视图)显示网页?

您需要创建一个WebViewClient对象并覆盖onPageFinished()方法。 然后使用webView.setWebViewClient()将您在上面创建的WebViewClient对象设置为WebView。 现在,您可以在onPageFinished()运行一些代码,例如setContentView() ,以便在加载完成后将内容视图从加载屏幕更改为Web视图。

本教程可能会对您有所帮助,尽管它并不完全符合您的需求。 http://www.giantflyingsaucer.com/blog/?p=1331

您必须使用事件onPageFinished()。

这是官方参考Clickme

你的主要活动必须是setContentView(R.layout.main); 而onPageFinished()函数必须是setContentView(webview);

你可以做这样的事情,在你的布局根框架布局(或相对布局)做这样的事情。

<Framelayout android:layout_height="fill_parent" android:layout_width="fill_parent"
....>

    <Webview android:layout_height="fill_parent" android:layout_width="fill_parent"/>

    <include android:layout_height="fill_parent" android:layout_width="fill_parent"  
    layout="@loadingLayout"/>

</Framelayout>

当Web视图完成加载后,只需将包含的组件可见性更改为Gone即可。

我希望它有帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM