天天看點

Android 設定字型、App字型和TextView字型、附字型包前言一、assets是什麼?二、APP全局字型三、單個TextView設定字型四、優秀的個性化字型功能架構

文章目錄

  • 前言
  • 一、assets是什麼?
  • 二、APP全局字型
    • 2.1.引入庫
    • 2.2.在Application中初始化calligraphy
    • 2.3.讓指定的Activity配置自定義字型
      • 2.3.1 Android Q 以及 Android X 開發環境報錯
  • 三、單個TextView設定字型
    • 3.1 使用Typeface + .ttf實作
    • 3.2 使用SDK自帶字型
      • 3.2.1 通過xml實作
      • 3.2.2 通過Java邏輯代碼實作
    • 3.3 使用RoBoto在xml設定字型
      • 3.3.1 xml中使用 android:fontFamily
      • 3.3.2 fontFamily參數屬性
  • 四、優秀的個性化字型功能架構
    • 4.1 通過Spannables設定文本樣式
    • 4.2 使關鍵詞帶有可點選的下劃線TextView
    • 4.3 用手勢縮放字型大小
    • 4.4 用顔色标記一些短語

前言

本文介紹Android實作全局設定自定義字型和局部設定自定義字型即單個TextView設定字型,同時也提供了一些優秀的三方字型架構,基本可以滿足開發者對字型設定的全部要求。

使用自定義字型前後效果圖

Android 設定字型、App字型和TextView字型、附字型包前言一、assets是什麼?二、APP全局字型三、單個TextView設定字型四、優秀的個性化字型功能架構

一、assets是什麼?

首先需要了解Android之assets

簡而言之,你的圖檔、svg檔案放在工程的res/drawabe下,則設定字型用到的字型檔案則位于assets下面。

如何建立assets目錄、點選進入

二、APP全局字型

2.1.引入庫

代碼如下(示例):

//用以設定App全局字型
implementation 'uk.co.chrisjenx:calligraphy:2.2.0'

           

2.2.在Application中初始化calligraphy

代碼如下(示例):

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //app字型
        CalligraphyConfig.initDefault(
                new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );

    }
    public static LightMeterApplication getInstance() {
        return instance;
    }
}


           

在AndroidManifest.xml配置自定義MyApplication以替代預設Application

<application
        android:name=".MyApplication"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"

        android:theme="@style/AppTheme">
           

下面是我的字型目錄

Android 設定字型、App字型和TextView字型、附字型包前言一、assets是什麼?二、APP全局字型三、單個TextView設定字型四、優秀的個性化字型功能架構

下載下傳字型,點選進入提取碼:2555

你也可以導入Windows自帶字型,

字型路徑:C:\Windows\Fonts

我的Win10自帶263種字型檔案,下面是字型檔案截圖

Android 設定字型、App字型和TextView字型、附字型包前言一、assets是什麼?二、APP全局字型三、單個TextView設定字型四、優秀的個性化字型功能架構

2.3.讓指定的Activity配置自定義字型

重寫下面這個方法

//不重寫的Activity還是安卓預設字型
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
           

2.3.1 Android Q 以及 Android X 開發環境報錯

如果你的項目更新了AndroidX環境以及 android Q 上調試時則會報以下錯誤 
           
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bigcat.edulearnaid, PID: 21204
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bigcat.edulearnaid/com.bigcat.edulearnaid.ui.StartActivity}: android.view.InflateException: Binary XML file line #17 in com.bigcat.edulearnaid:layout/abc_screen_simple: Binary XML file line #17 in com.bigcat.edulearnaid:layout/abc_screen_simple: Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3895)
           

解決方法:

在項目build.gradle中添加如下依賴替代 uk.co.chrisjenx:calligraphy:2.2.0

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
           
  • Application的onCreate()中初始化:
ViewPump.init(ViewPump.builder()
        	.addInterceptor(new CalligraphyInterceptor(
         		new CalligraphyConfig.Builder()
                    .setDefaultFontPath("你的字型")
                    .setFontAttrId(R.attr.fontPath)
                    .build()))
                .build());
           
  • BaseActivity中attachBaseContext的方法中
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}
           

三、單個TextView設定字型

和設定全局字型不同的是無需配置Application,無需引入依賴庫calligraphy,仍需配置字型路徑,使用下面的方法完成字型設定

3.1 使用Typeface + .ttf實作

protected Typeface tfRegular;//定義字型
{
tfRegular = Typeface.createFromAsset(getActivity().getAssets(), "fonts/OpenSans-Regular.ttf");//初始化字型
textView.setTypeface(tfRegular);
}

           

3.2 使用SDK自帶字型

noraml (普通字型,系統預設使用的字型)

sans(非襯線字型)

serif (襯線字型)

monospace(等寬字型)

3.2.1 通過xml實作

<TextView
	android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="580.6Ix"
    android:gravity="center"
    android:textStyle="bold"   
    android:textSize="20sp"
    android:typeface="serif"
    android:textColor="@color/white"/>
    <!--  android:textStyle="bold"字型加粗   -->
    <!--  android:letterSpacing="0.2" 字型增加間距   -->
    <!--  android:type 設定SDK自帶字型   -->
           

3.2.2 通過Java邏輯代碼實作

vSansText = (TextView) findViewById(R.id.sans);
        vSerifText = (TextView) findViewById(R.id.serif);
        vMonospaceText = (TextView) findViewById(R.id.monospace);
 
        //設定字型樣式
        vSansText.setTypeface(Typeface.SANS_SERIF);
        vSerifText.setTypeface(Typeface.SERIF);
        vMonospaceText.setTypeface(Typeface.MONOSPACE);
           

3.3 使用RoBoto在xml設定字型

通過xml實作自定義設定字型的還包括RoBoto,Android4.0後預設字型就使用了Roboto,下面介紹一下使用方法:

3.3.1 xml中使用 android:fontFamily

android:fontFamily="sans-serif" // roboto regular  
android:fontFamily="sans-serif-light" // roboto light  
android:fontFamily="sans-serif-condensed" // roboto condensed  
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)  
//in combination with  
android:textStyle="normal|bold|italic"
           

3.3.2 fontFamily參數屬性

字型 屬性特征
Regular 标準字型
Italic 字型傾斜
Bold 字型加粗
Bold-italic 加粗和傾斜
Light 無襯線體字型
Light-italic 無襯線斜體
Thin 細體
Thin-italic 細斜體
Condensed regular 用于文本裝潢、資訊展示、網頁設計、篆刻制模[cr]
Condensed italic 斜體版cr
Condensed bold 粗體版cr
Condensed bold-italic 粗斜體版cr

四、優秀的個性化字型功能架構

4.1 通過Spannables設定文本樣式

BabushkaText ★659 -

4.2 使關鍵詞帶有可點選的下劃線TextView

UnderLineLinkTextView ★327 -

4.3 用手勢縮放字型大小

PinchZoomTextView ★272 -

4.4 用顔色标記一些短語

ColorTextView ★214 -