【问题标题】:How to load a webpage offline in android [duplicate]如何在android中离线加载网页[重复]
【发布时间】:2014-07-14 06:56:06
【问题描述】:

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

我的布局文件是

<?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>

【问题讨论】:

    标签: android webview


    【解决方案1】:

    文件上要使用的任何文件一般都放在assets文件夹中。请参阅this documentation。这是一个有效的 sn-p。

    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();
            }
    

    【讨论】:

      【解决方案2】:

      将你的 html 文件放入你的 assets 文件夹并用

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-28
        • 2017-06-25
        • 1970-01-01
        • 2012-03-19
        • 1970-01-01
        • 2013-06-07
        相关资源
        最近更新 更多