WebView加载html代码简单应用

MainActivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = findViewById(R.id.web);
        String webUrl = "<p><img class=\"wscnph\" src=\"https://zkw.oss-cn-beijing.aliyuncs.com/mall/images/5f2421586efc4816ae355b4064f49b34.jpg\" /></p>";
        String JS = "<head>\n" +
                "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />\n" +
                "<style>p{margin:0}body{margin-left:0.1; margin-top:0.7; margin-right:0.1; margin-bottom:1;}img{width:100%; height:auto;}</style>\n" +
                "</head>";
        WebSettings settings = webView.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptEnabled(true);//支持JS
        settings.setDisplayZoomControls(false);
        settings.setDomStorageEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setMediaPlaybackRequiresUserGesture(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String regx = "<img( )?[a-zA-Z0-9.:\\\"' //=;%/,!_()-\uFE30-\uFFA05]+(/)?>";
                Pattern p = Pattern.compile(regx);
                Matcher matcher = p.matcher(webUrl);
                StringBuilder sb = new StringBuilder();
                while (matcher.find()) {
                    sb.append(matcher.group());
                }
                String content = sb.toString().replace("style", "").replace("align", "");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadDataWithBaseURL(null, "<html>" + JS + "<body>" + webUrl + "</body>" + "</html>",
                                "text/html", "utf-8", null);
                    }
                });
            }
        }).start();
    }
}

AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/web" />

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值