使用CSS更改的WebView字体;在资源文件夹的字体文件。 (问题) [英] Change WebView font using CSS; font file in asset folder. (problem)

查看:220
本文介绍了使用CSS更改的WebView字体;在资源文件夹的字体文件。 (问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变的WebView字体。有在资产文件夹2的文件。它们是:

资产/字体/ DejaVuSans.ttf和

资产/ CSS / result.css

我搜索的StackOverflow的一些解答,并找出一个解决方案,但它不工作。该result.css负荷DejaVuSans.ttf使用@字体面指令。然后它被包含与<链接>被送到的WebView :: loadDataWithBaseURL(在code低于)数据标签。 的问题是CSS样式的作品(文字为红色),但字体不。方块字当我告诉注音符号出现(/'pə:SN /).

 字符串resultText =< HTML>< HEAD>中;
resultText + =<链接HREF = \的CSS / result.css \+相对= \样式\类型= \文/ CSS \/>中;
resultText + =<风格>中+@字体面{字体家庭:\DejaVuSans \;++
SRC:网址(文件:///android_asset/fonts/DejaVuSans.ttf');}< /风格>中;
resultText + =< /头>中;
resultText + =<身体GT;< P>中+文字+< / P>中+< /身体GT;< / HTML>中;
resultView.loadDataWithBaseURL(文件:/// android_asset /,resultText,text / html的,UTF-8,NULL);
 

result.css:

  @字体面{
    字体家庭:DejaVuSans;
    / * SRC:网址(字体/ DejaVuSans.ttf');也无法正常工作* /
    SRC:网址(文件:///android_asset/fonts/DejaVuSans.ttf');
}

体 {
    颜色:红色;
}
 

本的System.out.println(resultText)是:

 < HTML>< HEAD><链接HREF =CSS / result.css相对=样式类型=文本/ CSS/><风格&GT ; @字体面{字体家庭:DejaVuSans; SRC:网址(文件:///android_asset/fonts/DejaVuSans.ttf'); }< /样式和GT;< /头><身体GT;< P>人/'pə:SN /< / P>< / BODY>< / HTML>
 

解决方案

这解决了。我没加字体系列身体选择和字体url必须的文件:///android_asset/fonts/DejaVuSans.ttf

  @字体面{
    字体家庭:DejaVuSans;
    / * SRC:网址(DejaVuSans.ttf');这不起作用* /
    / * SRC:网址(字体/ DejaVuSans.ttf');这也不是* /
    SRC:网址(文件:///android_asset/fonts/DejaVuSans.ttf'); / *但是这不* /
}

体 {
    字体家庭:DejaVuSans;
    颜色:红色;
}
 

I want to change WebView font. There are 2 files in "assets" folder. They are:

assets/fonts/DejaVuSans.ttf and

assets/css/result.css

I searched some answers on StackOverflow and find out a solution but it doesn't work. The result.css loads DejaVuSans.ttf using @font-face directive. Then it is included in < link > tag of data which is passed to WebView::loadDataWithBaseURL (The code is below). The problem is CSS styles works (the text is red) but the font doesn't. Square characters appears when I show phonetic symbols (/'pə:sn/).

String resultText = "<html><head>";
resultText += "<link href=\"css/result.css\"" + " rel=\"stylesheet\" type=\"text/css\" />";
resultText += "<style>" + "@font-face { font-family: \"DejaVuSans\"; " + "" +
"src: url('file:///android_asset/fonts/DejaVuSans.ttf'); }</style>";
resultText += "</head>";
resultText += "<body><p>" + text + "</p>" + "</body></html>";
resultView.loadDataWithBaseURL("file:///android_asset/", resultText, "text/html", "utf-8", null);

result.css:

@font-face { 
    font-family: "DejaVuSans"; 
    /*src: url('fonts/DejaVuSans.ttf'); also not work*/ 
    src: url('file:///android_asset/fonts/DejaVuSans.ttf');
}

body {
    color: red;
}

The System.out.println(resultText) is:

   <html><head><link href="css/result.css" rel="stylesheet" type="text/css" /><style>@font-face { font-family: "DejaVuSans"; src: url('file:///android_asset/fonts/DejaVuSans.ttf'); }</style></head><body><p>person /'pə:sn/</p></body></html>

解决方案

It was solved. I didn't add "font-family" to body selector and font url must be file:///android_asset/fonts/DejaVuSans.ttf".

@font-face { 
    font-family: 'DejaVuSans'; 
    /* src: url('DejaVuSans.ttf'); This doesn't work */
    /* src: url('fonts/DejaVuSans.ttf'); Neither is this */
    src: url('file:///android_asset/fonts/DejaVuSans.ttf'); /* but this does */
}

body {
    font-family: 'DejaVuSans';
    color: red;
}

这篇关于使用CSS更改的WebView字体;在资源文件夹的字体文件。 (问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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