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

Nia*_*yle 3 sqlite fonts android webview

我想在我的Android应用程序中嵌入自定义字体.我不使用的TextView所以这样的教程为这一个(如何使用与TextView的自定义字体)没有帮助.

在我的例子中,内容来自SQLite数据库,并使用WebView显示在屏幕上.我既不使用捆绑的HTML文件,所以本教程(如何使用WebView自定义字体)也无法解决我的问题.

FIY,这是我的代码:

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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢你.

Pau*_*Jan 9

回复中的评论中,可能的解决方案似乎是loadDataWithBaseURL在提供assets文件夹作为基本URL时使用,即

LoadData()不起作用,但webView.loadDataWithBaseURL("file:/// android_asset /",...工作正常.然后字体文件引用为"/fonts/MyFont.otf"应该有效. - JaakL Dec 1' 11点16分59秒

我假设捆绑你的字体不是问题,对吧?

[编辑]为了澄清我的答案,我写了一个小例子.在下面的代码,我已经把Quicksand_Dash.otf资产/字体,并twitter_logo.png直入资产.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>";


}
Run Code Online (Sandbox Code Playgroud)