android webview 加载本地文件

来源:互联网 发布:360电脑数据恢复软件 编辑:程序博客网 时间:2024/05/21 10:12

新建一个activity,里面放一个webview

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="webview.fanruitian.cn.webview.MainActivity">    <WebView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/webview"    /></android.support.constraint.ConstraintLayout>

开启访问网络权限

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="webview.fanruitian.cn.webview">    <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

主要是这句话

<uses-permission android:name="android.permission.INTERNET"/>

注意位置


在app/src/main/目录下建立文件夹asset

在asset目录下建立文件index.html,里面写

<h1>Helo,World</h1>

在oncreate方法里面添加

 webView = (WebView) findViewById(R.id.webview);        webView.getSettings().setJavaScriptEnabled(true);        webView.loadUrl("file:///android_asset/index.html");        webView.setWebViewClient(new WebViewClient(){//            @Override//            public void onPageFinished(WebView view, String url) {//                super.onPageFinished(view, url);//            }        });


测试成功

0 0
原创粉丝点击