AndroidWebView控件基本使用示例

Tanya ·
更新时间:2024-09-20
· 1117 次阅读

Android WebView用于在 android 中显示网页。可以从相同的应用程序或 URL 加载网页。它用于在 android 活动中显示在线内容。

Android WebView 使用 webkit 引擎来显示网页。

android.webkit.WebView 是 AbsoluteLayout 类的子类。

Android WebView 类的loadUrl()loadData()方法用于加载和显示网页。

Android WebView 示例

让我们看看使用 Web 视图显示 baidu.com 网页的简单代码。

WebView mywebview = (WebView) findViewById(R.id.webView1); mywebview.loadUrl("http://www.baidu.com/");

让我们看看使用 Web 视图显示 HTML 网页的简单代码。在这种情况下,html 文件必须位于资产目录中。

WebView mywebview = (WebView) findViewById(R.id.webView1); mywebview.loadUrl("file:///android_asset/myresource.html");

让我们看另一个显示字符串的 HTML 代码的代码

String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>"; mywebview.loadData(data, "text/html", "UTF-8"); 完整的 Android WebView 示例

让我们看一个完整的 Android WebView 示例。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>

要在应用程序中本地添加网页(.html、.jsp),需要将它们放置在 assets 文件夹中。资产文件夹创建为:右键单击应用程序 -> 新建 -> 文件夹 -> 资产文件夹 -> 主目录,或者简单地在主目录中创建资产目录。

MainActivity.java

package com.example.webview; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView mywebview = (WebView) findViewById(R.id.webView); mywebview.loadUrl("http://www.baidu.com"); //系统默认会通过手机浏览器打开网页,为了能够直接通过WebView显示网页,则必须设置 mywebview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //使用WebView加载显示url view.loadUrl(url); //返回true return true; } }); /* String data = "<html><body><h1>Hello, World!</h1></body></html>"; mywebview.loadData(data, "text/html", "UTF-8");*/ //mywebview.loadUrl("file:///android_asset/myresource.html"); } }

AndroidManifest.xml

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

如果加载 HTML 页面,让我们看看输出。

如果您加载 baidu.com 网页,让我们看看输出。

总结

到此这篇关于Android WebView控件基本使用示例的文章就介绍到这了,更多相关Android WebView控件内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



示例

需要 登录 后方可回复, 如果你还没有账号请 注册新账号