【问题标题】:Android Studio: How to set a webview loading and error view?Android Studio:如何设置 webview 加载和错误视图?
【发布时间】:2015-04-12 00:26:26
【问题描述】:

我正在为我的学习目的开发一个应用程序,该应用程序包含一个导航抽屉和一些 web 视图。 我的问题是当页面加载时,屏幕仍然是白色和无聊的。如果连接失败,则会出现丑陋的“无连接”页面,显示我的主机地址.. 我想设置一个加载页面或图像以及另一个用于替代方案(连接失败,...)我尝试了一些解决方案,但没有一个适合我的代码。

是片段JAVA和XML代码..

layout1_acceuil.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>​

menu1_acceuil.java

package xxxx.yyyyyy;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by Malek Boubakri on 13/03/2015.
 */
public class menu1_accueil extends Fragment {
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle setInitialSavedState) {
        rootview = inflater.inflate(R.layout.layout1_accueil, container, false);
        //set up webview
        WebView webView1 = (WebView) rootview.findViewById(R.id.webView1);
        // Enable Javascript
        WebSettings webSettings = webView1.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //load an URL
        webView1.loadUrl("http://myhost.tn");
        // Force links and redirects to open in the WebView instead of in a browser
        webView1.setWebViewClient(new WebViewClient());
        return rootview;
    }
}

对于一些天才来说,我的问题可能很容易,但我只是一个初学者,如果这个应用程序不运行,它将严重影响我的分数:'(请发布任何可以帮助我的信息,我将不胜感激。 (再次,我不得不提一下,我还是 android dev 的初学者,我的英语可能有点糟糕..)

【问题讨论】:

    标签: android android-layout android-fragments android-webview webviewclient


    【解决方案1】:

    您可以在使用 WebViewClient 类加载网页之前和之后执行任何操作。您可以覆盖 onPageStarted()、onPageFinished() 和 onReceivedError() 等方法。

    这是 WebViewClient 的 android 开发者网站,您可以在这里找到所有信息:http://developer.android.com/reference/android/webkit/WebViewClient.html

    【讨论】:

    • 好的,这个链接很有用,他们给出了方法和类的每一个细节,但他们没有解释如何使用!!我正在使用一个抽屉,这意味着 .MainActivity 中没有 webviews,当我在链接中使用这些方法时,如果出现错误:(
    • 也许这个链接对你有帮助,代码 sn-ps developer.android.com/guide/webapps/webview.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多