我們知道Android中,有各種不同尺寸的屏幕,所以,就需要多套UI圖片來進行手機的適配,這樣,又會造成apk過大。
所以,使用字體文件來替換掉UI圖片也屬于APK瘦身的一種方式。
本篇文章將介紹字體圖標庫的使用。
目錄
- 獲取ttf字體庫
- 使用ttf字體庫
1.獲取ttf字體庫
阿里巴巴提供了一個圖標庫Iconfont,我們可以去這里下載自己需要圖標。
當然也可以找UI做。
① 選中圖標加入購物車
② 下載文件
③ 獲取ttf字體庫
iconfont.ttf 圖標生成的字體文件
demo_unicode文件,可以看到里面圖標unicode編碼
2.使用ttf字體庫
①把 iconfont.ttf 字體庫 放到 assets目錄 下
②打開里面 demo_unicode文件,可以看到里面圖標 unicode編碼
③ 我們在values下面的string.xml中創建我們字體圖標
<resources>
<string name="icon_repair"></string>
</resources>
現在,我們就能在項目中,使用我們的字體圖標了,首先創建TextView
<TextView
android:id="@+id/tv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:textColor="@color/colorAccent"
android:text="@string/icon_repair"/>
④ 在代碼中為TextView設置字體文件
// 加載字體文件
Typeface typeface = Typeface.createFromAsset(getAssets(),"iconfont.ttf");
TextView mTxtView = (TextView) findViewById(R.id.textview);
// 為TextView設置字體文件
mTxtView.setTypeface(typeface);
結果展示: