【问题标题】:Use custom (unicode) fonts in WebView for my Android app在 WebView 中为我的 Android 应用使用自定义(unicode)字体
【发布时间】:2016-11-13 12:28:27
【问题描述】:

我想在我的 Android 应用程序中使用自定义字体,TextView 中的自定义字体可以正常工作,但我找不到在 WebView 中添加自定义(unicode)字体的方法,我已经尝试了所有可能的方法但失败了,看看我的代码,看看我是否遗漏了什么。谢谢!

我在资产文件夹中创建了一个带有自定义字体系列的 CSS 文件,并使用 LoadDataWithBaseURL 调用它,但它不起作用。

        htmlTextView = (WebView) findViewById(R.id.htmlTextView);

        String text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />";
        htmlTextView.loadDataWithBaseURL("file:///android_asset/", text, "text/html", "UTF-8", null);

        htmlTextView.getSettings().setJavaScriptEnabled(true);
        htmlTextView.setBackgroundColor(Color.TRANSPARENT);
        htmlTextView.getSettings().setDefaultFontSize(
                WebHelper.getWebViewFontSize(this));

CSS 文件:

@font-face {
  font-family: 'iskoola pota';
  src: url('fonts/iskpota.ttf')  format('truetype');
}

【问题讨论】:

    标签: java android unicode webview android-studio-2.1


    【解决方案1】:

    有一种方法可以将您的 html 代码放入 index.html 文件中

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

    html 看起来像这样。从资产文件夹调用您的 css 文件

    <html>
    <head><LINK href="file:///android_asset/css/myStyle.css" type="text/css" rel="stylesheet"/></head>
    <body>
        <img class="img" src="file:///android_asset/img/ok.png">
        <p > This above image is  shown from assets folder... </p>
        <p>Custom font <span style="color:red">'trado'</span> from asset folder has been applied on this text</p>
    </body>
    

    css 会是这个样子,你需要把你的字体文件放到asset文件夹中..

     @font-face {
        font-family: trado;
        src: url('file:///android_asset/fonts/trado.ttf');
    }
    

    希望它有效.. github上的完整示例click

    【讨论】:

    • 我不想使用 html 文件,我只想在我的基于 wordpress 的 Android 应用程序中使用自定义(unicode)字体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多