android - 如何将自定义字体嵌入到 Android 应用程序 (WebView)

标签 android sqlite fonts webview

我想在我的 Android 应用程序中嵌入自定义字体。我不使用 TextView,所以像 this one 这样的教程(How to use custom font with TextView)没有帮助。

在我的例子中,内容取自 SQLite 数据库并使用 WebView 显示在屏幕上。我也不使用 bundle 的 HTML 文件,所以 this tutorial (How to use custom font with WebView) 也没有解决我的问题。

请注意,这是我的代码:

public void initWebview()
{
    WebSettings settings = wvContent.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
    setContentView(R.layout.content);
    wvContent = (WebView) findViewById(R.id.wvContent);
    wvContent.setBackgroundColor(Color.argb(250, 250, 250, 250));
    wvContent.getSettings().setSupportZoom(true);  
    wvContent.getSettings().setBuiltInZoomControls(true);       
    wvContent.setInitialScale(100); 
    wvContent.setWebViewClient(new WebViewClient()

    {
        public void onPageFinished(WebView view, String url)
        {
            if (pd != null)
            {
                pd.dismiss();
                pd = null;
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            Log.i(CONTENT_TAG,"WebView link clicked; url = " + url);
            try
            {
                String arrUrlPart[] = url.split("://");

                if (arrUrlPart[0].equals("entry"))
                {
                    String content = getContentByWord(arrUrlPart[1]);
                    showContent(content);
                }
                else if (arrUrlPart[0].equals("http"))
                {
                     try {                             
                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));                              
                     } catch (Exception ex) {
                         // TODO Auto-generated catch block
                         ex.printStackTrace();
                     }                      
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            return true;
        }
    });
}

assets/fonts 中存储的字体似乎已嵌入到应用程序中,我的问题是:

  1. 如何以编程方式“强制”我的应用使用此字体?
  2. 我的问题有什么解决方案吗?

非常感谢。

最佳答案

来自 this 中的评论回复,一个可能的解决方案似乎是使用 loadDataWithBaseURL 同时提供 Assets 文件夹作为基本 url,即

LoadData() does not work, but webView.loadDataWithBaseURL("file:///android_asset/",... works fine. Then also font file reference as "/fonts/MyFont.otf" should work. – JaakL Dec 1 '11 at 16:59

我想 bundle 您的字体不是问题,对吧?

[编辑] 为了澄清我的回答,我编写了一个小例子。在下面的代码中,我将 Quicksand_Dash.otf 放入 assets/fonts,将 twitter_logo.png 直接放入 assets。 HTML 只是一个字符串常量,但您可以从 SQLLite 数据库中检索它。本质真的只是使用loadDataWithBaseURL()....

package oh.jolly.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebviewTestActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadDataWithBaseURL("file:///android_asset/", PAGE_HTML, "text/html", "UTF-8", "null" );
    }

    private final static String PAGE_HTML =
        "<html>\n" +
        "  <style type=\"text/css\"> \n" + 
        "   @font-face { \n" + 
        "       font-family: MyFont; \n" + 
        "       src: url(\"file:///android_asset/fonts/Quicksand_Dash.otf\") \n" + 
        "   } \n" + 
        "   body { \n" + 
        "       font-family: MyFont; \n" + 
        "       font-size: medium; \n" + 
        "       text-align: justify; \n" + 
        "   } \n" + 
        "  </style> \n" + 
        "  <body>\n" + 
        "    I've got a sinking feeling...<br>\n" + 
        "    <img src=\"file:///android_asset/twitter_logo.png\" />\n" + 
        "  </body>\n" + 
        "</html>";


}

关于android - 如何将自定义字体嵌入到 Android 应用程序 (WebView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237821/

相关文章:

android - 适用于 IOS 或 Android 的 Python

android - 无法同步Gradle项目/无法使用哈希字符串找到目标

ios - 了解 React Native 中的默认字体大小

java - Windows 和 Linux 加载 ttf 字体时 JLabel 的差异

css - Web 字体不能联机工作。但在本地工作

android - Android 服务中的计时器任务 VS 警报管理器用法

Android ActionBar/ActionBarSherlock 多项选择微调器

objective-c - 将数组存储到 SQLite 数据库中

sql - 如何对 ServiceStack 进行单元测试?

iphone - 使用后端数据构建 iOS 应用程序的最佳实践