VSCode部分(测试样例):
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>使用WebView调用在线网址(VSCode编写网页+Android Studio获取)</h3>
</body>
</html>
Android Studio部分:
布局文件:(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<WebView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
java文件:(Mainactivity)
关键代码如下:
webView.loadUrl("http://(写自己本地ip):5500/基本使用.html");
若直接使用
http://127.0.0.1:5500/%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8.html(该乱码是因为采用了中文直接复制,粘贴所得,不影响),会得到如下报错页面--WebView不显示。
更改为自己的ip地址后便可正常显示,要注意需要AndroidManifest.xml配置相关权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
正常显示如下所示:
以上便是对不能正确使用WebView显示在线网址的问题以及解决的方案,希望可以帮助到有需要的人,喜欢的可以点赞收藏噢~