- Font settings in TextView
- How to make text bold and italic?
- relationship between the three
- custom font
- refer to:
Font settings in TextView
There are three properties in textView to set the font
- textStyle: set the style
- fontFamily: set the font used
- typeface: Set the font file used.
textStyle as follows
- NORMAL: default font;
- BOLD: rough title;
- ITALIC: Italics;
- BOLD_ITALIC: bold italic;
typeface
The java system supports the following fonts by default:
- noraml (normal font, the font used by the system by default)
- sans (sans serif)
- serif (serif font)
- monospace (monospaced font)
Briefly introduce the difference between the three fonts: In the Roman alphabet camp of Western countries, fonts are divided into two categories: Sans Serif and Serif. Although the typewriter body also belongs to Sans Serif, but because it is a monospaced font, it is independent of Monospace. type.
Serif means that there are additional decorations at the beginning and end of the strokes of the characters, and the thickness of the strokes will vary from vertical to horizontal. Sans Serif, on the other hand, does not have these extra decorations, and the strokes are roughly the same thickness. As shown below: The difference between

Serif and Sans-serif fonts
There are two ways to set the font.
1. Configuration settings
<TextView
android:text="Hello,World"
android:textSize="20sp"
<!-- 使用默认的sans字体-->
android:typeface="sans"
<!-- 使用默认的serifs字体-->
android:typeface="serif"
<!-- 使用默认的monospace字体-->
android:typeface="monospace"
/>
Code mode settings
//设置serif字体
textView.setTypeface(Typeface.SERIF);
//设置sans字体
textView.setTypeface(Typeface.SANS_SERIF);
//设置monospace字体
textView.setTypeface(Typeface.MONOSPACE);
fontFamily
The fontFamily value is the font name supported by the system.
According to the typography chapter in the MATERIAL DESIGN documentation, anroid supports the following font styles:

The new version supports new fonts, see the table below for details.
These font files are defined in the data/fonts/fonts.xml folder of system files for android 5.0+ and above. Details connectable files.
<!-- first font is default -->
<family name="sans-serif">
<font weight="100" style="normal">Roboto-Thin.ttf</font>
<font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
<font weight="300" style="normal">Roboto-Light.ttf</font>
<font weight="300" style="italic">Roboto-LightItalic.ttf</font>
<font weight="400" style="normal">Roboto-Regular.ttf</font>
<font weight="400" style="italic">Roboto-Italic.ttf</font>
<font weight="500" style="normal">Roboto-Medium.ttf</font>
<font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
<font weight="900" style="normal">Roboto-Black.ttf</font>
<font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
<font weight="700" style="normal">Roboto-Bold.ttf</font>
<font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
</family>
android 4.4 - documentation is different and feels easier to understand. The Font families chapter in the introduction of android 4.1 has a more detailed introduction
Font families
Android 4.1 adds several more variants of the Roboto font style for a total of 10 variants, and they're all usable by apps. Your apps now have access to the full set of both light and condensed variants.
The complete set of Roboto font variants available is:
Regular
Italic
Bold
Bold-italic
Light
Light-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic
You can apply any one of these with the new fontFamily attribute in combination with the textStyle attribute.
Supported values for fontFamily are:
"sans-serif" for regular Roboto
"sans-serif-light" for Roboto Light
"sans-serif-condensed" for Roboto Condensed
You can then apply bold and/or italic with textStyle values "bold" and "italic". You can apply both like so: android:textStyle="bold|italic".
You can also use Typeface.create(). For example, Typeface.create("sans-serif-light", Typeface.NORMAL).
The font.xml file defines many fonts. What if the fonts do not support them? There is the following paragraph in the font.xml annotation of the android 5.0 version, which can be a good description of the processing flow of the system:
The first family is also the default font, which handles font request that have not specified
specific font names.
The font.xml file and its change records have relatively complete update records.
Here's a table that illustrates the problem well ( source ):
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 |

Summary: fontFamliy represents a list of fonts supported by the android system. Each font has an alias to uniquely identify the font. Due to the different versions of android, some font settings supported by the higher version will not take effect in the lower version, and the default font of the lower version will be used.
How to make text bold and italic?
We know that TextView draws text, and it is ultimately done through Paint-related settings.
A common requirement in android is to set the FontFamily property of TextView. There are two ways to set:
- Method 1 is done through the configuration file
android:fontFamily="serif"
- Method 2: Set by code.
TextView.setTypeface(TypeFace typeface)。
public void setTypeface(Typeface tf) {
//...其他代码
mTextPaint.setTypeface(tf);
//...更新界面代码
}
You can see the specific implementation of the setTypeface method of TexTView.
TextView.setTypeface(TypeFace typeface, int style)。
public void setTypeface(Typeface tf, int style) {
if (style > 0) {
if (tf == null) {
tf = Typeface.defaultFromStyle(style);
} else {
tf = Typeface.create(tf, style);
}
setTypeface(tf);
// now compute what (if any) algorithmic styling is needed
int typefaceStyle = tf != null ? tf.getStyle() : 0;
int need = style & ~typefaceStyle;
mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
} else {
mTextPaint.setFakeBoldText(false);
mTextPaint.setTextSkewX(0);
setTypeface(tf);
}
}
- Paint.setFakeBoldText method realizes bold font
- Paint.setTextSkewX implements font italics
relationship between the three
There is a private method in TextView:setTypefaceFromAttrs
private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
Typeface tf = null;
if (familyName != null) {
tf = Typeface.create(familyName, styleIndex);
if (tf != null) {
setTypeface(tf);
return;
}
}
switch (typefaceIndex) {
case SANS:
tf = Typeface.SANS_SERIF;
break;
case SERIF:
tf = Typeface.SERIF;
break;
case MONOSPACE:
tf = Typeface.MONOSPACE;
break;
}
setTypeface(tf, styleIndex);
}
in conclusion:
- Both fontFamily and typeface are used to set the font, and d is set to use fontFamily first.
- textStyle is used to change the style of the font
custom font
android O started to support custom fonts
https://developer.android.com/guide/topics/resources/font-resource.html