android:fontFamily的有效值以及它们所对应的内容?

在对这个问题的回答中,用户列出了android:fontFamily和12个变体的值(见下文)。这些值是怎么来的?`android:fontFamily'的文档中没有任何地方列出这些信息(我检查了这里这里)。这些字符串在Android的style.xml文件中的各个地方都有列出,但是这些字符串是如何映射到Roboto字体的?

从安卓4.1/4.2开始,下列Roboto字体家族是 可用。

android:fontFamily="sans-serif" // roboto regular android:fontFamily="sans-serif-light" // roboto light android:fontFamily="sans-serif-condensed" // roboto condensed android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

与此结合使用

android:textStyle="normal|bold|italic"

可以有12种变体。

  • 普通
  • 斜体
  • 粗体 *粗斜体
  • 浅色
  • 轻度斜体
  • 薄 *薄型大写字母 浓缩的正体 凝练斜体
  • 浓缩的粗体
  • 浓缩的黑体-大写字母

在我工作的应用程序的styles.xml文件中,有人将此列为字体家族,我非常确定这是错误的。

<item name="android:fontFamily"> Roboto-Regular.ttf</item>

我想为我们的应用程序正确设置主题(包括正确使用fontFamily),并删除所有冗余的样式,这些样式是在我看了文件之前创建的。

解决办法

这些值从哪里来?android:fontFamily的文档并没有在任何地方列出这些信息

这些确实没有在文档中列出。但在'字体家族一节下提到了它们这里。该文档列出了Android Jelly Bean 4.1的每个新的公共API。

在我正在开发的应用程序的style.xml文件中,有人将其列为字体家族,而我非常确定这是错误的。

是的,这是错的。你不能引用字体文件,你必须使用上述链接文件中提到的字体名称。在这种情况下,它应该是这样的。

sans-serif

就像链接的答案已经说过的,有12种变体是可能的。

在Android Jelly Bean (4.1)中添加 - API 16:

常规(默认)。

sans-serif
normal 

意大利语

sans-serif
italic

粗体

sans-serif
bold

粗体字:

sans-serif
bold|italic

:

sans-serif-light
normal

轻量级:

:

sans-serif-thin
normal

薄-大写

sans-serif-thin
italic

凝练的普通

sans-serif-condensed
normal

精简斜体:

sans-serif-condensed
italic

浓缩的黑体

sans-serif-condensed
bold

浓缩的黑体字

sans-serif-condensed
bold|italic

在Android Lollipop(v5.0)中添加 - API 21:

中度

sans-serif-medium
normal

中英文对照

黑色:

sans-serif-black
italic

为了快速参考,它们都是这样的。

评论(25)

可用的字体(截至奥利奥)

![所有字体预览][1]

Material Design Typography]2 页面有其中一些字体的演示,以及关于选择字体和样式的建议。

对于代码探究者来说。fonts.xml 是权威的、不断扩展的Android字体列表。


使用这些字体

设置android:fontFamilyandroid:textStyle属性,例如。


到这个表中的理想值。

Font                     | android:fontFamily          | android:textStyle
-------------------------|-----------------------------|-------------------
Roboto Thin              | sans-serif-thin             |
Roboto Light             | sans-serif-light            |
Roboto Regular           | sans-serif                  |
Roboto Bold              | sans-serif                  | bold
Roboto Medium            | sans-serif-medium           |
Roboto Black             | sans-serif-black            |
Roboto Condensed Light   | sans-serif-condensed-light  |
Roboto Condensed Regular | sans-serif-condensed        |
Roboto Condensed Medium  | sans-serif-condensed-medium |
Roboto Condensed Bold    | sans-serif-condensed        | bold
Noto Serif               | serif                       |
Noto Serif Bold          | serif                       | bold
Droid Sans Mono          | monospace                   |
Cutive Mono              | serif-monospace             |
Coming Soon              | casual                      |
Dancing Script           | cursive                     |
Dancing Script Bold      | cursive                     | bold
Carrois Gothic SC        | sans-serif-smallcaps        |

(Noto Sans是一种后备字体;你不能直接指定它)

注意:_本表源自fonts.xml。每个字体的家族名称和样式都列在fonts.xml中,例如。


    CutiveMono.ttf

因此,serif-monospace是字体家族,normal是样式。


兼容性

根据fonts.xml的log和以前的system_fonts.xml,你可以看到每种字体是什么时候添加的。

  • 冰激凌三明治:Roboto常规、粗体、斜体和粗斜体
  • 果冻豆:Roboto light, light italic, condensed, condensed bold, condensed italic, and condensed bold italic。
  • 果冻豆MR1:Roboto薄和薄斜体
  • Lollipop: Roboto中号、中号斜体、黑色和黑色斜体 Noto Serif regular, bold, italic, bold italic *Cutive Mono
    • 即将推出
    • 舞蹈书
    • Carrois Gothic SC *诺托无衬线
  • Oreo MR1:Roboto 浓缩中号字体
评论(4)

就我所知,你不能在xml或主题中声明自定义字体。我通常只是做一些自定义类来扩展textview,在实例化时设置自己的字体,并在我的布局xml文件中使用这些字体。

即。

public class Museo500TextView extends TextView {
    public Museo500TextView(Context context, AttributeSet attrs) {
        super(context, attrs);      
        this.setTypeface(Typeface.createFromAsset(context.getAssets(), "path/to/font.ttf"));
    }
}

评论(1)