本篇主要講述的是通過XML文件進(jìn)行全局字體的修改。文章內(nèi)容大部分來自Android官方文檔。Fonts in XML
Android 8.0(API 26)加入了一個(gè)新特性 - Fonts in XML,此特性允許將字體作為資源進(jìn)行加載,使我們可以更方便的設(shè)置全局的字體樣式。添加的方式是在 app/res/font 文件夾下添加字體文件(.otf或.ttf文件),然后就像平時(shí)使用資源文件的方式使用字體資源。例如,@font/myfont或R.font.myfont。
前提條件
1.這種通過資源文件修改字體的方式只能在運(yùn)行Android 4.1(API級(jí)別16)及更高版本的設(shè)備上才能生效。
2.并且你的App需要使用Support Library 26及以上版本。
先給大家看下最終的實(shí)際使用與效果
<TextView
style="@style/MaliTextStyle"
android:text="@string/title"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text"
style="@style/MaliTextStyle"
android:layout_marginTop="20dp"
android:text="@string/text1"
android:textSize="15sp"
android:textStyle="normal" />
<TextView
style="@style/MaliTextStyle"
android:layout_marginTop="20dp"
android:text="@string/text2"
android:textSize="15sp"
android:textStyle="italic" />

開始敲代碼
1.右鍵單擊res文件夾,然后轉(zhuǎn)到“ New”>“Android Resource Directory”。
將出現(xiàn)“ New Resource Directory”窗口。-
2.在“Resource type”列表中,選擇“ font”,然后單擊“ 確定”。
創(chuàng)建一個(gè)新的字體資源目錄.png -
3.在字體文件夾中添加字體文件。
創(chuàng)建font文件夾,復(fù)制.ttf文件.png
如果你使用的是Android Studio,雙擊字體文件可以直接預(yù)覽字體。我這里的字體都是在網(wǎng)上下載的,英文字體文件可以在Google Fonts獲取字體文件(需要翻墻)。我選擇了其中的兩種字體做個(gè)示例:Merriweather 與 Mali。
這里需要注意,下載的字體文件都是大寫加空格的,需要按照資源文件命名規(guī)范進(jìn)行修改。
- 4.創(chuàng)建字體系列(font-family)
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<font
android:font="@font/merriweather_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/merriweather_regular"
app:fontStyle="normal"
app:fontWeight="400"
tools:targetApi="o" />
<font
android:font="@font/merriweather_italic"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/merriweather_italic"
app:fontStyle="normal"
app:fontWeight="400"
tools:targetApi="o" />
</font-family>
字體系列是一組字體文件及其樣式和權(quán)重的詳細(xì)信息。有時(shí),解壓縮字體文件夾,并發(fā)現(xiàn)多個(gè)版本的字體相同,如斜體版本,或重量和厚度不同的字體。 如果不知道具體的權(quán)重可以在Google Fonts的下載頁面看到。字體權(quán)重.png
如果是中文字體,權(quán)重都設(shè)置為100即可。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<!--這里引用的字體是中文字體-->
<font
android:font="@font/pingfang_regular"
android:fontStyle="normal"
android:fontWeight="100"
app:font="@font/pingfang_regular"
app:fontStyle="normal"
app:fontWeight="100"
tools:targetApi="o"/>
<font
android:font="@font/pingfang_regular"
android:fontStyle="italic"
android:fontWeight="100"
app:font="@font/pingfang_regular"
app:fontStyle="italic"
app:fontWeight="100"
tools:targetApi="o"/>
</font-family>
使用font-family就可以同個(gè)一個(gè)xml來配置管理定義字體的每個(gè)版本,以及相關(guān)聯(lián)的樣式和權(quán)重屬性。
- 5.在布局文件中使用
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/font_merriweather" />
到這里為止,通過XML修改字體已經(jīng)完成了。
下面講一講通過style來更方便的設(shè)置字體,也讓布局文件更精簡(jiǎn)。
1.編寫width 與 height 的style
2.編寫XXXTextStyle,繼承Layout_Wrap_Wrap,在Style中還可以設(shè)置字間距、行間距,對(duì)應(yīng)屬性分別是:letterSpacing、lineSpacingExtra
3.XXXTextStyle還可以用在所有繼承自TextView編寫的View中,例如:Button、EditText
4.將TextSize 與 TextColor也寫在Style中,這樣可以規(guī)范XML中文本相關(guān)的樣式編寫。
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Layout_Wrap_Wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="Layout_Match_Wrap">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="Layout_Match_Match">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="Layout_Wrap_Match">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="MaliTextStyle" parent="Layout_Wrap_Wrap">
<item name="android:fontFamily">@font/font_mali</item>
<item name="android:letterSpacing" tools:targetApi="lollipop">0.05</item>
</style>
<style name="MerriweatherTextStyle" parent="Layout_Wrap_Wrap">
<item name="android:fontFamily">@font/font_merriweather</item>
<item name="android:letterSpacing" tools:targetApi="lollipop">0.05</item>
</style>
<style name="TextStyle_10_Gray87868B" parent="MaliTextStyle">
<item name="android:textSize">@dimen/sp_10</item>
<item name="android:textColor">@color/app_gray_87868B</item>
</style>
</resources>
<!--布局文件中使用-->
<TextView
style="@style/TextStyle_10_Gray87868B"
android:text="@string/title"/>
這樣的Style復(fù)用性強(qiáng),拓展性強(qiáng),代碼閱讀性也強(qiáng)
以上的代碼已經(jīng)上傳到github:示例代碼


