资源字体没有加载在android webView [英] Asset font is not loaded in android webView

查看:163
本文介绍了资源字体没有加载在android webView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android活动,它正在加载一个HTML文件到webview。
不过,此动作不会在部分手机上载入字型。 HTC Desire或索尼Xperia Z与4.4或4.1机器人。
我想知道我是否错过了某个项目,或者只是取决于我测试我的应用程序的手机。

  private void loadToWebView(String s){
try {
pageWebView.loadDataWithBaseURL(file:/// android_asset /,s,
text / html,utf-8 , 空值);
configureWebView(pageWebView);
} catch(Exception e){
e.printStackTrace();
}
}

这里是html标题:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/ TR / xhtml1 / DTD / xhtml1-transitional.dtd> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta content =text / html; charset = utf-8http-equiv =Content-Type/>
< style type =text / css>
@ font-face {font-family:'Persian'; src:url('file:///android_asset/fonts/b_yekan.ttf'); }
@ font-face {font-family:'Persian2'; src:url('file:///android_asset/fonts/b_homa.ttf'); }
@ font-face {font-family:'PersianTitle'; src:url('file:///android_asset/fonts/b_titr.ttf');}
body {font-family:'Persian';}
h1 {font-family:'PersianTitle'; }
h2 {font-family:'Persian2';}
< / style>

字体位于assets / fonts / like this $ $ b < =https://i.stack.imgur.com/O9CXU.pngalt =enter image description here>

解决方案


  • 首先,字体路径应该与HTML / CSS文件相关。



所以,而不是这样:

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

使用这样的:

  @ font-face {font-family:'Persian'; src:url('fonts / b_yekan.ttf'); 




  • 第二您测试的目标实际上支持阿拉伯语脚本。



下面我提供一个工作示例。






assets / about_us.html

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta content =text / html; charset = utf-8http-equiv =Content-Type/>
< style type =text / css>
@ font-face {font-family:'B Homa'; src:url('fonts / BHOMA.TTF');}
@ font-face {font-family:'B Lotus'; src:url('fonts / BLOTUS.TTF');}
@ font-face {font-family:'B Lotus Bold'; src:url('fonts / BLOTUSBD.TTF');}
< / style>
< / head>
< body>
< p style =font-family:'B Homa'; font-size:20px;> B Homa:مخصص< / p>
< p style =font-family:'B Lotus'; font-size:20px;> B Lotus:مخصص< / p&
< p style =font-family:'B Lotus Bold'; font-size:20px;> B Lotus Bold:مخصص< / p&
< / body>
< / html>

BHOMA.TTF BLOTUS.TTF BLOTUSBD.TTF 是从 bornaray.com 下载的,并存储在<$ c $ ,它是<$ c $ 的一部分。 c> Fragment ,因此 getActivity()。getAssets()

  private void loadToWebView(){
AssetManager assetManager = getActivity()。getAssets();
try {
InputStream input = assetManager.open(about_us.html);
byte [] buffer = new byte [input.available()];
input.read(buffer);
input.close();
mWebView.loadDataWithBaseURL(file:/// android_asset /,
new String(buffer),text / html,UTF-8,null);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}





希望这有帮助。


I'm developing an android activity which is loading an HTML file into a webview.
However this acitivty is not loading fonts in some phones e.g. HTC Desire or Sony Xperia Z with 4.4 or 4.1 androids.
I want to know if i have missed something or it is only depends on phone which i'm testing my app.

private void loadToWebView(String s) {
    try {
        pageWebView.loadDataWithBaseURL("file:///android_asset/", s,
                "text/html", "utf-8", null);
        configureWebView(pageWebView);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

here is html header:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
@font-face { font-family: 'Persian'; src: url('file:///android_asset/fonts/b_yekan.ttf'); }
@font-face { font-family: 'Persian2'; src: url('file:///android_asset/fonts/b_homa.ttf'); }
@font-face { font-family: 'PersianTitle'; src: url('file:///android_asset/fonts/b_titr.ttf');}
body {font-family: 'Persian';}
h1 {font-family: 'PersianTitle';}
h2 {font-family: 'Persian2';}
</style>

fonts are located in assets/fonts/ like this

解决方案

  • First of all, fonts path should be relative to your HTML/CSS file.

So, instead of this:

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

Use something like this:

@font-face { font-family: 'Persian'; src: url('fonts/b_yekan.ttf');

  • Second, you must make sure that the target you are testing against actually supports Arabic script.

With that, below I am providing a working example.


assets/about_us.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
    @font-face { font-family: 'B Homa'; src: url('fonts/BHOMA.TTF');}
    @font-face { font-family: 'B Lotus'; src: url('fonts/BLOTUS.TTF');}
    @font-face { font-family: 'B Lotus Bold'; src: url('fonts/BLOTUSBD.TTF');}
</style>
</head>
<body>
    <p style="font-family:'B Homa';font-size:20px;">B Homa: مخصص</p>
    <p style="font-family:'B Lotus';font-size:20px;">B Lotus: مخصص</p>
    <p style="font-family:'B Lotus Bold';font-size:20px;">B Lotus Bold: مخصص</p>
</body>
</html>

BHOMA.TTF, BLOTUS.TTF and BLOTUSBD.TTF are downloaded from bornaray.com and stored in assets/fonts/ folder.

loadToWebView(), it is part of a Fragment, hence getActivity().getAssets():

private void loadToWebView() {
    AssetManager assetManager = getActivity().getAssets();
    try {
        InputStream input = assetManager.open("about_us.html");
        byte[] buffer = new byte[input.available()];
        input.read(buffer);
        input.close();
        mWebView.loadDataWithBaseURL("file:///android_asset/",
                new String(buffer), "text/html", "UTF-8", null);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And the result:

Hope this helps.

这篇关于资源字体没有加载在android webView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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