如何改变web视图的字体面对Android的? [英] How to change font face of Webview in Android?

查看:164
本文介绍了如何改变web视图的字体面对Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变的WebView的默认字体自定义字体。我使用的WebView在为Android开发一个双语的浏览器应用程序。

I want change the default font of webview to a custom font. I'm using webview in developing an bilingual browser app for Android.

我试图把我的自定义字体的资产获取自定义字体的一个实例。但还是没能设置的WebView的默认字体我的字体。

I tried getting an instance of custom typeface by placing my custom font in assets. But still couldn't set webview's default font to my font.

这是我的尝试:

Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf"); 
private WebView webview;
WebSettings webSettings = webView.getSettings();
webSettings.setFixedFontFamily(font);

任何人都可以解决这个或建议的任何其它方法来改变的WebView的默认字体自定义字体?

Can anyone correct this or suggest any other method to change webview's default font to a custom font?

谢谢!

推荐答案

有这样的的这个项目。它归结为:

There's a working example of this in this project. It boils down to:

  1. 资产/字体文件夹,将需要的光学传递函数或TTF字库(这里MyFont.otf)
  2. 创建,你会使用web视图的内容的HTML文件,在资产文件夹(这里在资产/演示/ my_page.html ):

  1. In your assets/fonts folder, place the desired OTF or TTF font (here MyFont.otf)
  2. Create a HTML file that you'll use for the WebView's content, inside the assets folder (here inside assets/demo/my_page.html):

<html>
<head>
<style type="text/css">
@font-face {
    font-family: MyFont;
    src: url("file:///android_asset/fonts/MyFont.otf")
}
body {
    font-family: MyFont;
    font-size: medium;
    text-align: justify;
}
</style>
</head>
<body>
Your text can go here! Your text can go here! Your text can go here!
</body>
</html>

  • 加载HTML到从code中的web视图:

  • Load the HTML into the WebView from code:

    webview.loadUrl("file:///android_asset/demo/my_page.html");
    

  • 请留意透过 loadData()注入HTML 是不允许的。 <一href="http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String,%20java.lang.String,%20java.lang.String)">As每个文档:

    Take note that injecting the HTML through loadData() is not permitted. As per the documentation:

    需要注意的是JavaScript的同源策略意味着脚本中使用此方法加载的网页运行将无法访问使用任何不是'数据'等方式加载的内容,包括HTTP(S)。为了避免这种限制,使用loadDataWithBaseURL()用合适的碱的URL。

    Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.

    由于@JaakL显示在下面的评论,加载HTML从一个字符串,你应该将提供基本URL指向您的资产:

    As @JaakL suggests in the comment below, for loading HTML from a string, you should instead provide the base URL pointing to your assets:

    webView.loadDataWithBaseURL("file:///android_asset/", htmlData);
    

    当引用的 htmlData 字体,您就可以简单地使用 /fonts/MyFont.otf (省略基本URL)。

    When referencing the font in htmlData, you may then simply use /fonts/MyFont.otf (omitting the base URL).

    这篇关于如何改变web视图的字体面对Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆