ChatGPT解决这个技术问题 Extra ChatGPT

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.


a
arekolek

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:

https://developer.android.com/design/media/typography_variants@2x.png


Haha :D I actually knew about the site before that, so finding it wasn't difficult. But for future reference: I prefer to use the search on developer.android.com over the google search, since you can limit the search to only the API's, blog posts, release notes etc.
And if you are interested: This is where the fonts are defined. This class loads the fonts and this one manages them afaict.
@androiddeveloper done. Roboto black does not refer to the color of the font, but is just a "more bolder" version of of Roboto Bold. I've added a reference image at the bottom.
@Ahmad Thank you for clarifying this. Here's your +1 ... :)
also, there is a lot of alias to use with fontFamily android.googlesource.com/platform/frameworks/base/+/master/data/…
N
Newtonx

Available fonts (as of Oreo)

https://i.stack.imgur.com/MM70p.png

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, e.g.

<!-- 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, e.g.

<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

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


how did you find on the log about the Android version ?
@androiddeveloper I was looking at the dates when each line was added. To know precisely which fonts are available in a particular version, look at system_fonts.xml for that version, e.g. for Lollipop: android.googlesource.com/platform/frameworks/base/+/…
My one and only gripe about this is it is not alphabetized :p But still! This should be the best answer! Thank you!
A
Ahmad

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

This can cost you a lot of memory in a recyclerview