Valid values for android:fontFamily and what they map to?

In the answer to this question the user lists values for android:fontFamily and 12 variants (see below). Where do these values come from? The documentation for android:fontFamily does not list this information in any place (I checked here, and here). The strings are listed in the Android styles.xml file in various places, but how do these map back to the Roboto font?

From android 4.1 / 4.2, the following Roboto font families are available:

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)

in combination with this

android:textStyle="normal|bold|italic" 

12 variants are possible:

  • Regular
  • Italic
  • Bold
  • Bold-italic
  • Light
  • Light-italic
  • Thin
  • Thin-italic
  • Condensed regular
  • Condensed italic
  • Condensed bold
  • Condensed bold-italic
  • In the styles.xml file in the application I'm working on somebody listed this as the font family, and I'm pretty sure it's wrong:

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

    I'd like to get the theme for our app set up correctly (which includes using fontFamily correctly) and remove all the redundancy that is in some of the styles that were created before I had a look at the file.


    Where do these values come from? The documentation for android:fontFamily does not list this information in any place

    These are indeed not listed in the documentation. But they are mentioned here under the section 'Font families'. The document lists every new public API for Android Jelly Bean 4.1.

    In the styles.xml file in the application I'm working on somebody listed this as the font family, and I'm pretty sure it's wrong:

    Yes, that's wrong. You don't reference the font file, you have to use the font name mentioned in the linked document above. In this case it should have been this:

    <item name="android:fontFamily">sans-serif</item>
    

    Like the linked answer already stated, 12 variants are possible:

    Added in Android Jelly Bean (4.1) - API 16 :

    Regular (default):

    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">normal</item> 
    

    Italic :

    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">italic</item>
    

    Bold :

    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold</item>
    

    Bold-italic :

    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold|italic</item>
    

    Light :

    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
    

    Light-italic :

    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">italic</item>
    

    Thin :

    <item name="android:fontFamily">sans-serif-thin</item>
    <item name="android:textStyle">normal</item>
    

    Thin-italic :

    <item name="android:fontFamily">sans-serif-thin</item>
    <item name="android:textStyle">italic</item>
    

    Condensed regular :

    <item name="android:fontFamily">sans-serif-condensed</item>
    <item name="android:textStyle">normal</item>
    

    Condensed italic :

    <item name="android:fontFamily">sans-serif-condensed</item>
    <item name="android:textStyle">italic</item>
    

    Condensed bold :

    <item name="android:fontFamily">sans-serif-condensed</item>
    <item name="android:textStyle">bold</item>
    

    Condensed bold-italic :

    <item name="android:fontFamily">sans-serif-condensed</item>
    <item name="android:textStyle">bold|italic</item>
    

    Added in Android Lollipop (v5.0) - API 21 :

    Medium :

    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textStyle">normal</item>
    

    Medium-italic :

    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textStyle">italic</item>
    

    Black :

    <item name="android:fontFamily">sans-serif-black</item>
    <item name="android:textStyle">italic</item>
    

    For quick reference, this is how they all look like:


    Available fonts (as of Oreo)

    预览所有字体

    The Material Design Typography page has demos for some of these fonts and suggestions on choosing fonts and styles.

    For code sleuths: fonts.xml is the definitive and ever-expanding list of Android fonts.


    Using these fonts

    Set the android:fontFamily and android:textStyle attributes, eg

    <!-- Roboto Bold -->
    <TextView
        android:fontFamily="sans-serif"
        android:textStyle="bold" />
    

    to the desired values from this table:

    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 is a fallback font; you can't specify it directly)

    Note: this table is derived from fonts.xml . Each font's family name and style is listed in fonts.xml, eg

    <family name="serif-monospace">
        <font weight="400" style="normal">CutiveMono.ttf</font>
    </family>
    

    serif-monospace is thus the font family, and normal is the style.


    Compatibility

    Based on the log of fonts.xml and the former system_fonts.xml, you can see when each font was added:

  • Ice Cream Sandwich: Roboto regular, bold, italic, and bold italic
  • Jelly Bean: Roboto light, light italic, condensed, condensed bold, condensed italic, and condensed bold italic
  • Jelly Bean MR1: Roboto thin and thin italic
  • Lollipop:
  • Roboto medium, medium italic, black, and black italic
  • Noto Serif regular, bold, italic, bold italic
  • Cutive Mono
  • Coming Soon
  • Dancing Script
  • Carrois Gothic SC
  • Noto Sans
  • Oreo MR1: Roboto condensed medium

  • As far as I'm aware, you can't declare custom fonts in xml or themes. I usually just make custom classes extending textview that set their own font on instantiation and use those in my layout xml files.

    ie:

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

    and

    <my.package.views.Museo900TextView
            android:id="@+id/dialog_error_text_header"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp" />
    
    链接地址: http://www.djcxy.com/p/24608.html

    上一篇: 阻止来自Root Cloak的应用程序隐藏根访问权限

    下一篇: android:fontFamily的有效值及其映射到的内容?