主页布局文件
<?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:orientation="vertical">
<WebView
android:id="@+id/wv_web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
主页代码
public class MainActivity extends AppCompatActivity {
private WebView wv_web;
private static final String URL = "网页链接";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv_web = findViewById(R.id.wv_web);
//打开加载JavaScript
wv_web.getSettings().setJavaScriptEnabled(true);
wv_web.setWebChromeClient(new WebChromeClient());
wv_web.loadUrl(URL);
}
}
AndroidManifest.xml添加网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
android9.0默认不适用http协议,如需开启请创建一个新的xml允许http协议连接,配置如下
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
AndroidManifest,xml中application添加android:networkSecurityConfig配置
<application
android:networkSecurityConfig="@xml/network_security_config">
</application>