如何嵌入一个自定义的字体Android应用(web视图)自定义、视图、字体、Android

2023-09-05 04:52:50 作者:烟、刺痛眼眸

我想嵌入自定义字体为我的Andr​​oid应用程序。我不使用的TextView所以这样的教程为this 之一(如何使用自定义的字体与TextView的)没有帮助。

I want to embed a custom font into my android app. I don't use TextView so such tutorials as this one (How to use custom font with TextView) do not help.

在我的情况下,内容是从SQLite数据库,并使用web视图显示在屏幕上。我不要么使用捆绑的HTML文件,以便本教程(如何使用自定义的字体,WebView功能)并没有解决我的问题,无论是。

In my case, the content is taken from the SQLite database and shown on the screen using WebView. I don't either use bundled HTML files so this tutorial (How to use custom font with WebView) does not solve my problem, either.

FIY,这里是我的code:

FIY, here is my code:

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;
        }
    });
}

存储在资产/字体字体似乎被嵌入到应用程序,我的问题是:

The font stored in assets/fonts seems to be embedded into the app, and my questions are:

我如何以编程方式做逼我的应用程序使用这种字体? 有没有我的任何问题的解决方案? How can I programmatically do to "force" my app to use this font? Are there any solutions to my problem?

非常感谢你。

推荐答案

从评论这个的回复,一个可能的解决办法似乎是使用 loadDataWithBaseURL 同时提供资产文件夹作为基本URL,即

From the comments in this reply, a possible solution seems to be to use loadDataWithBaseURL while providing the assets folder as the base url, i.e.

LoadData()不工作,但   webView.loadDataWithBaseURL(文件:/// android_asset /,......工作正常。   然后还字体文件引用为/fonts/MyFont.otf应该工作。 -   JaakL 12月1日'11在16:59

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

我想捆绑你的字体的是没有问题的,对吧?

I assume bundling your font is not a problem, right?

要澄清我的回答,我已经由一个小例子。在下面的code,我已经把 Quicksand_Dash.otf 中的资产/字体和 twitter_logo。 PNG 直入的资产的。 HTML的是一个简单的字符串常量,但你会从你的SQLLite数据库中检索它。其实质是真的只是用 loadDataWithBaseURL() ...

To clarify my answer, I've composed a little example. In the code below, I've put the Quicksand_Dash.otf in assets/fonts, and twitter_logo.png straight into assets. The HTML is simply a string constant, but you'd retrieve it from your SQLLite database. The essence is really just to use 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>";


}