微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Android Studio WebView 中使用 MathJax?

如何解决在 Android Studio WebView 中使用 MathJax?

好的,所以我正在尝试将 WebView 与使用 MathJax 库的本地 html 文件一起使用,但是当我运行它时,它不使用该库。我厌倦了 webView.setJavaScriptEnabled(),我确保它可以单独作为 html 文档而不是在 WebView 中使用。

这是我的代码

package com.example.mathcompetitions;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    WebView webView;

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


        webView = findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.loadUrl("file:///android_asset/index.html");

    }


}

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <Meta charset="utf-8" />
    <title>Math Concepts</title>
    <style>
        @font-face{
            font-family:Cambria;
            src:url('file:///android_asset/cambria.ttc')
        }
        body {
            font-family: 'Cambria';
            font-size: 32px;
            font-weight: normal;
            text-align: center;
            background-color: #222;
            color: #eee
        }
    </style>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"> </script>
</head>
<body>
<p id="p">
    When \(a \ne 0\),there are two solutions to \(ax^2 + bx + c = 0\) and they are
    \[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]
</p>
</body>
</html>

解决方法

切换到不同的 API 应该可以解决问题。在研究这个时,我是 29,但切换到 30 修复了它。正如清单中的评论 <uses-permission android:name="android.permission.INTERNET" /> 所提到的

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。