搬砖日记

搬砖日记

Customizing fonts for Android apps

When using the built-in font on Android devices:#

<!--Set the style in your app's theme or the theme of the place where you need to use it-->
<style name="CustomStyle" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/CustomFontStyleText</item>
    <item name="android:buttonStyle">@style/CustomFontStyleButton</item>
</style>

<!--Set the font style in the desired control-->
<style name="CustomFontStyleText" parent="android:Widget.TextView">
    <item name="android:fontFamily">Your font name</item>
</style>
<style name="CustomFontStyleButton" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">Your font name</item>
</style>

Note that if your theme's parent theme is an AppCompat theme, such as Theme.AppCompat.Light.DarkActionBar, you need to remove "android:" when setting android:fontFamily. In other words, replace the second line above with:

<item name="android:fontFamily">CustomStyle</item>

Then, set the theme in the Manifest file:

<!--If you set it as the application's theme-->
<application
    android:theme="@style/AppTheme" >
</application>

When using a custom font:#

Often, the runtime environment of your users is not consistent, so you need to include the font you need in your development in advance. To do this, create a "font" folder in your resource folder.

Usage:

<style name="CustomStyle" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="android:fontFamily">@font/Your font</item>
   <item name="fontFamily">@font/Your font</item>
</style>

Then, set the theme in the Manifest file:

<!--If you set it as the application's theme-->
<application
    android:theme="@style/AppTheme" >
</application>

Note:
Support Library 26.0 supports the "Fonts in XML" feature on devices running Android 4.1 (API level 16) and higher. For more custom usage methods, you can refer to the official documentation: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml


These are some methods for changing fonts. There are many other methods online, such as iterating through controls to set the font or setting the font for custom views. These methods can all be used, but in actual testing, some fonts may work fine, but resource usage is high when used globally. Therefore, the second method listed above is recommended.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.