Android O,通過資源文件全局修改字體,字間距,行間距

本篇主要講述的是通過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" />
運(yùn)行效果.png

開始敲代碼

  • 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:示例代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評(píng)論 25 708
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,110評(píng)論 2 59
  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,232評(píng)論 3 119
  • 對(duì)于粵菜,記憶最深的是廣東小伙伴的煲湯之旅,無論何時(shí)都有它的身影。而對(duì)于偏好重麻重辣的小伙伴來說,一提到吃粵菜,腦...
    食游集閱讀 530評(píng)論 0 0
  • 今天月初! 屁顛屁顛的把房間全部弄干凈整潔,還種了風(fēng)雨蘭 五點(diǎn)半就出去開攤,準(zhǔn)備拿廣告牌的...
    南瓜南生閱讀 157評(píng)論 0 0

友情鏈接更多精彩內(nèi)容