Keywords: Android | TextView | Font Setting | Typeface | Font Family
Abstract: This article provides an in-depth exploration of various methods for setting fonts in Android TextView, including using system-predefined Roboto font families, adding custom fonts via XML resources, and programmatically setting Typeface. It details the available sans-serif font variants in Android 4.1 and above, offering complete code examples and best practice guidelines to help developers flexibly control text display effects in their applications.
Utilizing System Predefined Font Families
In Android development, configuring TextView fonts is a common requirement. Starting from Android 4.1 (API level 16), the system provides a series of predefined Roboto font families that developers can directly use in XML layout files without needing to import additional font files.
The system predefined font families mainly include the following variants:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:fontFamily="sans-serif"
android:textSize="18sp" />
Available font family options include: sans-serif (Roboto regular), sans-serif-light (Roboto light), sans-serif-condensed (Roboto condensed), sans-serif-black (Roboto black), sans-serif-thin (Roboto thin, Android 4.2+), and sans-serif-medium (Roboto medium, Android 5.0+).
Combining Font Styles and Weights
By combining with the android:textStyle attribute, more font variant combinations can be created. The textStyle supports normal, bold, and italic values, which can be used in combination with various font families.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold Text Example"
android:fontFamily="sans-serif-light"
android:textStyle="bold"
android:textSize="18sp" />
This combination approach can produce 16 different font variants, including regular, italic, bold, bold italic, and variants corresponding to various font weights.
XML Configuration of Font Resources
To better manage font references in code, it's recommended to create a fonts.xml file in the res/values directory to define font resources:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>
This allows using fonts in layout files through @string resource references:
android:fontFamily="@string/font_family_light"
Custom Fonts via XML Resources
Starting from Android 8.0 (API level 26), using fonts as XML resources is supported. First, create a font folder in the res directory, then place font files in this folder.
Create XML configuration for font families:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/myfont_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/myfont_italic" />
<font
android:fontStyle="normal"
android:fontWeight="700"
android:font="@font/myfont_bold" />
</font-family>
Using custom fonts in layout files:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Example"
android:fontFamily="@font/my_font_family"
android:textSize="18sp" />
Programmatic Font Setting
In addition to XML methods, fonts can also be set dynamically in code. For Android 8.0 and above:
// Kotlin implementation
val typeface = resources.getFont(R.font.my_custom_font)
textView.typeface = typeface
// Java implementation
Typeface typeface = getResources().getFont(R.font.my_custom_font);
textView.setTypeface(typeface);
For scenarios requiring backward compatibility, use the Support Library:
// Kotlin implementation (Support Library)
val typeface = ResourcesCompat.getFont(context, R.font.my_custom_font)
// Java implementation (Support Library)
Typeface typeface = ResourcesCompat.getFont(context, R.font.my_custom_font);
Traditional Assets Folder Approach
Before the introduction of font XML resources, the common method was to place font files in the assets folder:
// Kotlin implementation
val font = Typeface.createFromAsset(assets, "custom_font.ttf")
textView.typeface = font
// Java implementation
Typeface font = Typeface.createFromAsset(getAssets(), "custom_font.ttf");
textView.setTypeface(font);
Best Practices and Compatibility Considerations
When choosing font setting methods, consider the application's compatibility requirements. If the application needs to support Android 4.1 and above, it's recommended to use the Support Library's font XML resource approach, which provides the best compatibility and maintainability.
For applications supporting only higher versions, you can directly use Android system's font XML resource functionality. When using custom fonts, pay attention to font file copyright issues and consider the impact of font file size on application package volume.
In actual development, it's recommended to manage font settings uniformly through styles, allowing reuse of the same font configuration across multiple TextViews and improving code maintainability.