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 网页,让我们看看输出。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值