在Android上使用CSS font-family选择Droid字体无效

6

我发现以下HTML代码在安卓上无法正常工作(只能使用默认字体:Droid Sans)。但在桌面端可以正常工作。

<p style='font-family: "Droid Sans",sans-serif;'>vând whisky și tequila, preț fix.</p>
<p style='font-family: "Droid Sans Mono",monospace;'>vând whisky și tequila, preț fix.</p>
<p style='font-family: "Droid Serif",serif;'>vând whisky și tequila, preț fix.</p>
2个回答

1

你的CSS中可能没有为其他字体声明@font-face。我想你只为Droid Sans声明了一个。你需要为每种字体都声明一个。例如,在你的CSS中应该有:

@font-face {
    font-family: 'DroidSans';
    src: url('font/DroidSans-webfont.eot?') format('eot'),
         url('font/DroidSans-webfont.woff') format('woff'),
         url('font/DroidSans-webfont.ttf') format('truetype'),
         url('font/DroidSans-webfont.svg#webfontw7zqO19G') format('svg');
    font-weight: normal;
    font-style: normal;

}

@font-face {
    font-family: 'DroidSansMono';
    src: url('font/DroidSans-Mono-webfont.eot?') format('eot'),
         url('font/DroidSans-Mono-webfont.woff') format('woff'),
         url('font/DroidSans-Mono-webfont.ttf') format('truetype'),
         url('font/DroidSans-Mono-webfont.svg#webfontSOhoM6aS') format('svg');
    font-weight: normal;
    font-style: normal;

}

@font-face {
    font-family: 'DroidSerif';
    src: url('font/DroidSerif-webfont.eot?') format('eot'),
         url('font/DroidSerif-webfont.woff') format('woff'),
         url('font/DroidSerif-webfont.ttf') format('truetype'),
         url('font/DroidSerif-webfont.svg#webfontw7zqO19G') format('svg');
    font-weight: normal;
    font-style: normal;

}

然后在你的代码中,font-family 应该与你在上面声明的 CSS 中相同(即 font-family: 'Droid Sans'; 不同于 font-family: 'DroidSans')

<p style='font-family: "DroidSans",sans-serif;'>vând whisky și tequila, preț fix.</p>
<p style='font-family: "DroidSansMono",monospace;'>vând whisky și tequila, preț fix.</p>
<p style='font-family: "DroidSerif",serif;'>vând whisky și tequila, preț fix.</p>

试一下,看看你能做到什么程度。


顺便提一下,您还需要确保在CSS中使用了正确的字体。在上面的示例中,所有字体文件都应该放在名为“fonts”的CSS子目录中。 - tadywankenobi
6
我不打算使用自定义字体,只需使用浏览器提供的字体。 - sorin
@tadywankenobi,Android标签不在那里是因为字体的原因 ;) - SparK

1
似乎Android只能识别system_fonts.xmlfonts.xml在gingerbread之前)。例如,要获取Droid Serif字体,您应该编写通用的serif。有关详细信息,请参见this answer
哦,还有一个与“Droid Sans”相关的怪癖:自4.0以来,该名称确实在配置文件中,但.ttf是指向Roboto的符号链接。
<speculation>我可以看出,在Android的早期,有人说“我们只会有三种字体,所以最好开发人员只使用通用名称,这样我们总是可以替换它们”</speculation>
但这很不幸。实际上,您将获得不同字体的原因有很多:
  • 在4.0版本之前您可以得到Droid字体族,之后您可以得到Roboto字体族。
  • Firefox正在捆绑Open Sans和Charis SIL并使用它们。
  • 制造商可能会用自己的字体替换这3种字体。编辑:我找不到任何确认此事实际发生的证据。

在所有这些情况下,如果我能够通过实际名称引用特定的字体堆栈,那将是很好的。然而,我不能这样做。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接