Android WebView控件基本使用示例

 更新时间:2022年01月11日 14:50:36   作者:allway2  
大家好,本篇文章主要讲的是Android WebView控件基本使用示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览

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控件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android中TabLayout+ViewPager 简单实现app底部Tab导航栏

    Android中TabLayout+ViewPager 简单实现app底部Tab导航栏

    TabLayout 是Android com.android.support:design库的一个控件。本文主要给大家介绍TabLayout+ViewPager 简单实现app底部Tab布局,需要的的朋友参考下
    2017-02-02
  • Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解

    Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解

    这篇文章主要介绍了Android BottomNavigationView结合ViewPager实现底部导航栏步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • Android ListView的item背景色设置和item点击无响应的解决方法

    Android ListView的item背景色设置和item点击无响应的解决方法

    在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的item默认的外观。
    2013-11-11
  • Android存储访问框架的使用小结

    Android存储访问框架的使用小结

    这篇文章主要介绍了Android存储访问框架的使用,存储访问框架API和MediaStore API的差异,在于存储访问框架API,是基于系统文件选择框的,用户选择了文件,那么相当于授权了, 可以访问所有类型的文件,需要的朋友可以参考下
    2022-01-01
  • 解决Android加壳过程中mprotect调用失败的原因分析

    解决Android加壳过程中mprotect调用失败的原因分析

    本文探讨的主要内容是mprotect调用失败的根本原因,以及在加壳实现中的解决方案,通过本文的阐述,一方面能够帮助遇到同类问题的小伙伴解决心中的疑惑,另一方面能够给大家提供可落地的实现方案,需要的朋友可以参考下
    2022-01-01
  • kotlin实现通知栏提醒功能示例代码

    kotlin实现通知栏提醒功能示例代码

    这篇文章主要给大家介绍了关于kotlin实现通知栏提醒功能的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用kotlin具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • Android自定义View实现圆环进度条

    Android自定义View实现圆环进度条

    这篇文章主要为大家详细介绍了Android自定义View实现圆环进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • Android中TelephonyManager类的用法案例详解

    Android中TelephonyManager类的用法案例详解

    这篇文章主要介绍了Android中TelephonyManager类的用法,以获取Android手机硬件信息为例详细分析了TelephonyManager类的使用技巧,需要的朋友可以参考下
    2015-09-09
  • Android中两个Activity之间数据传递及返回问题

    Android中两个Activity之间数据传递及返回问题

    本篇文章主要介绍了Android中两个Activity之间数据传递及返回问题,这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-02-02
  • Android仿iOS实现侧滑返回功能(类似微信)

    Android仿iOS实现侧滑返回功能(类似微信)

    这篇文章主要为大家详细介绍了Android仿iOS实现侧滑返回功能,类似微信功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论