如何在android中离线加载网页

时间:2014-05-25 10:16:55

标签: android webview

我正在尝试加载我在我的应用程序中处理的应用的条款和条件。客户端为我提供了条款和条件的html文件,我没有任何类型的服务器访问权限。所以从技术上讲,应用程序需要随身携带该文件。我已经完成了Web视图,其中在Web视图中显示了在线数据,但我没有任何结果。我知道我在这里错过了一个愚蠢的观点。请帮忙

我的布局文件是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

将您的html文件放入您的资源文件夹并使用

加载它
WebView wv= (WebView)findViewById(R.id.webview);
wv.loadUrl("file:///android_asset/yourhtml.html");

请注意。对于20126 Android Studio。对于新项目,要创建“资产文件夹”,右键单击“项目”中的“app”,然后向下钻取到“新资产文件夹”,然后按照向导进行操作。随后您可以将html(或其他)文件放入操作系统中的该文件夹;片刻之后,AndroidStudio会识别文件并在“项目”面板中显示它们。

答案 1 :(得分:2)

文件中使用的任何文档通常都放在assets文件夹中。请参阅this documentation。这是一个工作片段。

try {
            WebView mWebView = null;
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl("file:///android_asset/index.html"); 
        } catch (Exception e) {
            // TODO: handle exception
            this.finish();
        }
相关问题