淺析Android加載字體包及封裝的方法
TextView加載字體包
在 Android 中,若需要使得某個(gè)TextView加載字體包,使用以下方式即可:
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Bold.otf"); textView.setTypeface(typeFace);
至于字體包的位置:

通過(guò)以上方法,可以使得一個(gè)TextView加載某種字體包,但是,還有這種需求:
- 部分
TextView加載字體包 - 每個(gè)
TextView加載的字體包不一定一樣
這時(shí),我們就需要稍微封裝下,將其封裝成一個(gè)自定義TextView類(lèi),若需要使用字體包,則加載該類(lèi),同時(shí),可以根據(jù)xml里面的值,從而加載不同的字體包。
封裝
定義屬性值
首先,我們需要從xml里面獲取值,因此,需要在attr中進(jìn)行屬性值的定義:

<declare-styleable name="FontTextView"> <attr name="fontType" format="enum"> <enum name="bold" value="1" /> <enum name="heavy" value="2" /> </attr> </declare-styleable>
這里我只定義了兩種屬性,大家可以根據(jù)需求進(jìn)行增減。
創(chuàng)建自定義TextView
public class FontTextView extends AppCompatTextView {
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
獲取屬性值
//獲取參數(shù)
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FontTextView, defStyleAttr, 0);
int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);
進(jìn)行值判斷并加載不同的字體包
private final int BOLD = 1;
private final int HEAVY = 2;
String fontPath = null;
switch (fontType) {
case BOLD:
fontPath = "fonts/Bold.otf";
break;
case HEAVY:
fontPath = "fonts/Heavy.otf";
break;
default:
}
//設(shè)置字體
if (!TextUtils.isEmpty(fontPath)) {
Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath);
setTypeface(typeFace);
}
全部源碼
public class FontTextView extends AppCompatTextView {
private final int BOLD = 1;
private final int HEAVY = 2;
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//獲取參數(shù)
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FontTextView, defStyleAttr, 0);
int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);
String fontPath = null;
switch (fontType) {
case BOLD:
fontPath = "fonts/Bold.otf";
break;
case HEAVY:
fontPath = "fonts/Heavy.otf";
break;
default:
}
//設(shè)置字體
if (!TextUtils.isEmpty(fontPath)) {
Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath);
setTypeface(typeFace);
}
}
}
若需要使用字體包TextView,使用以下方式即可:
<com.jm.core.common.widget.textview.FontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:fontType="bold" android:text="測(cè)試" />
效果

到此這篇關(guān)于淺析Android加載字體包及封裝的方法的文章就介紹到這了,更多相關(guān)android加載字體包封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android webView字體突然變小的原因及解決
- Android使用TypeFace設(shè)置TextView的文字字體
- Android開(kāi)發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼
- Android開(kāi)發(fā)之FloatingActionButton懸浮按鈕基本使用、字體、顏色用法示例
- 詳解android 中文字體向上偏移解決方案
- Android如何動(dòng)態(tài)調(diào)整應(yīng)用字體大小詳解
- Android Studio設(shè)置、改變字體和主題的方法
- android 更改TextView中任意位置字體大小和顏色的方法
- Android修改字體樣式的示例代碼
- Android字體相關(guān)知識(shí)總結(jié)
相關(guān)文章
Android自定義可點(diǎn)擊的ImageSpan并在TextView中內(nèi)置View
這篇文章主要為大家詳細(xì)介紹了Android自定義可點(diǎn)擊的ImageSpan并在TextView中內(nèi)置"View",具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android自定義加載loading view動(dòng)畫(huà)組件
這篇文章主要為大家詳細(xì)介紹了Android自定義加載loading view動(dòng)畫(huà)組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android仿美團(tuán)分類(lèi)下拉菜單實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android仿美團(tuán)分類(lèi)下拉菜單實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android數(shù)字華容道小游戲開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Android數(shù)字華容道小游戲開(kāi)發(fā)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android調(diào)用手機(jī)攝像頭拍照和錄音功能
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用手機(jī)攝像頭拍照和錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
這篇文章主要介紹了 Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android WebView控件捕獲用戶(hù)輸入的信息
這篇文章主要為大家詳細(xì)介紹了Android WebView控件捕獲用戶(hù)輸入的信息,感興趣的小伙伴們可以參考一下2016-02-02

