gpt4 book ai didi

android - 使用 CSS 更改 WebView 字体; Assets 文件夹中的字体文件。 (问题)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:06 25 4
gpt4 key购买 nike

我想更改 WebView 字体。 “ Assets ”文件夹中有 2 个文件。它们是:

assets/fonts/DejaVuSans.ttf 和

Assets /css/result.css

我在 StackOverflow 上搜索了一些答案并找到了解决方案,但它不起作用。 result.css 使用 @font-face 指令加载 DejaVuSans.ttf。然后它包含在传递给 WebView::loadDataWithBaseURL 的数据的 标记中(代码如下)。 问题是 CSS 样式有效(文本为红色)但字体无效。当我显示注音符号(/'pə:sn/)时出现方 block 字符。

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

结果.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;
}

System.out.println(resultText) 是:

   <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>

最佳答案

解决了。我没有将“font-family”添加到正文选择器,字体 url 必须是 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;
}

关于android - 使用 CSS 更改 WebView 字体; Assets 文件夹中的字体文件。 (问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com