Android開發(fā)--更換字體

  • 開發(fā)中,經(jīng)常遇到需要更換字體格式,設(shè)計(jì)妹子為了讓UI更美,設(shè)置了很多讓人著(tong)迷(ku)的字體,但是藍(lán)瘦歸藍(lán)瘦,還是得擼起袖子開干。
  • Android系統(tǒng)中,默認(rèn)提供三種字體:"sans", "serif", "monospace"

如果設(shè)置字體為系統(tǒng)字體之一,在XML中,直接設(shè)置字體格式:
1、sans

  <TextView
            Android:id="@+id/sans"
            Android:text="sans"
            Android:textSize="10sp"
            Android:typeface="sans" />```
2、monospace

<TextView
Android:id="@+id/monospace"
Android:text="monospace"
Android:textSize="10sp"
Android:typeface="monospace" />```

3、serif

  <TextView
            Android:id="@+id/serif"
            Android:text="serif"
            Android:textSize="10sp"
            Android:typeface="serif" />```


- ###### 使用自定義字體
首先要引入自定義的字體文件,例如引入Roboto-Light字體,將Roboto-Light.ttf放入assets\fonts\目錄下

![引入字體文件.png](http://upload-images.jianshu.io/upload_images/2789715-fab431d67c790407.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1、一般情況下,只針對部分字體,或者某個(gè)界面的少量字體進(jìn)行修改
在XML布局中,不做任何修改:

<TextView
Android:id="@+id/textview "
Android:text="custom"
Android:textSize="10sp" />```
在代碼中:
得到TextView對象

TextView textView =(TextView)findViewById(R.id.textview);```
創(chuàng)建Typeface對象

Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Roboto-Light.ttf");```
設(shè)置字體

textView.setTypeface(typeFace);```
簡單的三步,搞定。

2、上面是針對部分字體修改,但有時(shí)設(shè)計(jì)為了App的整體美觀或者App產(chǎn)品本身的定位,絕大部分甚至所有字體,都需要使用自定義的字體。
如果再使用上面的方法逐一修改,對開發(fā)者來說無異于噩耗,而且,這種毫無意義的重復(fù)勞動(dòng),也不符合我們能懶就懶得程序員風(fēng)格
那就想點(diǎn)省事的方法吧:
(1)、獲取系統(tǒng)字體,并替換

public final class FontsOverride {

public static void setDefaultFont(Context context,
        String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typeface.createFromAsset(context.getAssets(),
            fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

protected static void replaceFont(String staticTypefaceFieldName,
        final Typeface newTypeface) {
    try {
        final Field staticField = Typeface.class
                .getDeclaredField(staticTypefaceFieldName);
        staticField.setAccessible(true);
        staticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

}```
在Application類中替換系統(tǒng)默認(rèn)字體

public final class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FontsOverride.setDefaultFont(this, "DEFAULT", "Roboto-Light.ttf");
    }
}```

(2)、上面是全局替換,但更多時(shí)候開發(fā)中只需要替換一部分,甚至有時(shí)想在XML中設(shè)置字體:
自定義CustomTextView對象

public class CustomTextView extends TextView {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomTextView(Context context) {
super(context);
init();
}

private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
setTypeface(tf);
}
}```
XML中使用自定義的CustomTextView

  <com.packagename.CustomTextView
                    android:id="@+id/custom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Custom TextView"
                    android:textappearance="?android:attr/textAppearanceLarge"/>```

總結(jié):如果使用Android系統(tǒng)提供字體,直接在XML布局中設(shè)置TextView的typeface;
          如果使用自定義字體,則根據(jù)具體需求,選擇對應(yīng)的方法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,537評論 0 17
  • RelativeLayout 第一類:屬性值為true可false android:layout_centerHr...
    兀兀沙彌閱讀 3,058評論 0 15
  • 歡迎Follow我的GitHub, 關(guān)注我的CSDN. 其余參考Android目錄. 轉(zhuǎn)載請注明出處:http:/...
    passiontim閱讀 4,810評論 0 31
  • 今年我二十歲,大一,大家是不是感覺有點(diǎn)晚?我也覺得有點(diǎn)晚!因?yàn)檫@點(diǎn),我很多時(shí)候總是在夜晚睡不著,想一些不著邊際的東...
    798書屋閱讀 462評論 1 2
  • 愿我們摒棄掉一切,在一個(gè)陌生的地方,來一場跨越,身高,年齡,距離。可歌可泣的愛情,只為愛你。 愿我們在夕陽下牽手漫...
    二十楊朱泣歧路閱讀 332評論 1 0