ifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<!-- 互联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 存储卡 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_web_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
<WebView
android:id="@+id/wv_assets_web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity:
package com.example.myapplication;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
private String mFilePath = "file:///android_asset/html/index.html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv_web_path = findViewById(R.id.tv_web_path);
// 从布局文件中获取名叫wv_assets_web的网页视图
WebView wv_assets_web = findViewById(R.id.wv_assets_web);
tv_web_path.setText("下面网页来自资产文件:" + mFilePath);
// 命令网页视图加载指定路径的网页
wv_assets_web.loadUrl(mFilePath);
// 给网页视图设置默认的网页浏览客户端
wv_assets_web.setWebViewClient(new WebViewClient());
}
}