
- Android 基础
- Android - 首页
- Android - 概述
- Android - 环境搭建
- Android - 架构
- Android - 应用组件
- Android - Hello World 示例
- Android - 资源
- Android - 活动 (Activities)
- Android - 服务 (Services)
- Android - 广播接收器 (Broadcast Receivers)
- Android - 内容提供器 (Content Providers)
- Android - 碎片 (Fragments)
- Android - 意图/过滤器 (Intents/Filters)
- Android - 用户界面
- Android - UI 布局
- Android - UI 控件
- Android - 事件处理
- Android - 样式和主题
- Android - 自定义组件
- Android 高级概念
- Android - 拖放
- Android - 通知
- 基于位置的服务
- Android - 发送电子邮件
- Android - 发送短信
- Android - 电话拨打
- 发布 Android 应用
- Android 实用示例
- Android - 警报对话框
- Android - 动画
- Android - 音频捕捉
- Android - 音频管理器 (AudioManager)
- Android - 自动完成
- Android - 最佳实践
- Android - 蓝牙
- Android - 相机
- Android - 剪贴板
- Android - 自定义字体
- Android - 数据备份
- Android - 开发者工具
- Android - 模拟器
- Android - Facebook 集成
- Android - 手势
- Android - Google 地图
- Android - 图片特效
- Android - ImageSwitcher
- Android - 内部存储
- Android - JetPlayer
- Android - JSON 解析器
- Android - LinkedIn 集成
- Android - 加载旋转器
- Android - 本地化
- Android - 登录界面
- Android - MediaPlayer
- Android - 多点触控
- Android - 导航
- Android - 网络连接
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 进度圆圈
- Android - 进度条
- Android - 推送通知
- Android - RenderScript
- Android - RSS 阅读器
- Android - 屏幕录制
- Android - SDK 管理器
- Android - 传感器
- Android - 会话管理
- Android - 共享首选项
- Android - SIP 协议
- Android - 拼写检查器
- Android - SQLite 数据库
- Android - 支持库
- Android - 测试
- Android - 文字转语音
- Android - TextureView
- Android - Twitter 集成
- Android - UI 设计
- Android - UI 模式
- Android - UI 测试
- Android - WebView 布局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 实用资源
- Android - 问答
- Android - 实用资源
- Android - 讨论
Android - WebView
WebView 是一种在你的应用程序中显示网页的视图。你也可以指定 HTML 字符串,并使用 WebView 在你的应用程序中显示它。WebView 将你的应用程序转变为一个 web 应用程序。
为了将 WebView 添加到你的应用程序中,你必须将 <WebView> 元素添加到你的 xml 布局文件中。其语法如下:
<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
为了使用它,你必须在 Java 文件中获取此视图的引用。要获取引用,请创建一个 WebView 类的对象。其语法如下:
WebView browser = (WebView) findViewById(R.id.webview);
为了将 web url 加载到 WebView 中,你需要调用 WebView 类的 loadUrl(String url) 方法,并指定所需的 url。其语法如下:
browser.loadUrl("https://tutorialspoint.com");
除了加载 url 之外,你还可以使用 WebView 类中定义的方法来更好地控制你的 WebView。它们列举如下:
序号 | 方法和描述 |
---|---|
1 |
canGoBack() 此方法指定 WebView 是否有后退历史记录项。 |
2 |
canGoForward() 此方法指定 WebView 是否有前进历史记录项。 |
3 |
clearHistory() 此方法将清除 WebView 的前进和后退历史记录。 |
4 |
destroy() 此方法销毁 WebView 的内部状态。 |
5 |
findAllAsync(String find) 此方法查找所有字符串实例并突出显示它们。 |
6 |
getProgress() 此方法获取当前页面的进度。 |
7 |
getTitle() 此方法返回当前页面的标题。 |
8 |
getUrl() 此方法返回当前页面的 url。 |
如果你点击 WebView 网页中的任何链接,该页面不会在你的 WebView 中加载。为此,你需要从 WebViewClient 扩展你的类并重写其方法。其语法如下:
private class MyBrowser extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }
示例
这是一个演示 WebView 布局用法的示例。它创建一个基本的 web 应用程序,将要求你指定一个 url,并在 WebView 中加载此 url 网站。
要试验此示例,你需要在一个正在运行互联网的实际设备上运行它。
步骤 | 描述 |
---|---|
1 | 你将使用 Android Studio 在 com.example.sairamkrishna.myapplication 包下创建一个 Android 应用程序。 |
2 | 修改 src/MainActivity.java 文件以添加 WebView 代码。 |
3 | 修改 res/layout/activity_main 以添加相应的 XML 组件 |
4 | 修改 AndroidManifest.xml 以添加必要的权限 |
5 | 运行应用程序,选择一个正在运行的 Android 设备,将应用程序安装在其上并验证结果。 |
以下是修改后的主活动文件 src/MainActivity.java 的内容。
package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button b1; EditText ed1; private WebView wv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); ed1=(EditText)findViewById(R.id.editText); wv1=(WebView)findViewById(R.id.webView); wv1.setWebViewClient(new MyBrowser()); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = ed1.getText().toString(); wv1.getSettings().setLoadsImagesAutomatically(true); wv1.getSettings().setJavaScriptEnabled(true); wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); wv1.loadUrl(url); } }); } private class MyBrowser extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
以下是修改后的 xml 文件 res/layout/activity_main.xml 的内容。
在下面的代码中,abc 表示 tutorialspoint.com 的徽标
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="WebView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="Enter Text" android:focusable="true" android:textColorHighlight="#ff7eff15" android:textColorHint="#ffff25e6" android:layout_marginTop="46dp" android:layout_below="@+id/imageView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/imageView" android:layout_alignEnd="@+id/imageView" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_toRightOf="@+id/imageView" android:layout_toEndOf="@+id/imageView" /> <WebView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/webView" android:layout_below="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" /> </RelativeLayout>
以下是 res/values/string.xml 的内容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 文件的内容。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行你的 WebView 应用程序。要从 Android Studio 运行应用程序,请打开你的项目中的一个活动文件,然后单击工具栏中的运行 图标。Android Studio 将显示如下所示:
现在只需在 url 字段中指定一个 url 并按下出现的浏览按钮即可启动网站。但在那之前,请确保你已连接到互联网。按下按钮后,将出现以下屏幕:

注意:只需更改 url 字段中的 url,你的 WebView 就会打开你想要的网站。

上图显示了 tutorialspoint.com 的 webview。