-
開發(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\目錄下

1、一般情況下,只針對(duì)部分字體,或者某個(gè)界面的少量字體進(jìn)行修改
在XML布局中,不做任何修改:
<TextView
Android:id="@+id/textview "
Android:text="custom"
Android:textSize="10sp" />```
在代碼中:
得到TextView對(duì)象
TextView textView =(TextView)findViewById(R.id.textview);```
創(chuàng)建Typeface對(duì)象
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Roboto-Light.ttf");```
設(shè)置字體
textView.setTypeface(typeFace);```
簡單的三步,搞定。
2、上面是針對(duì)部分字體修改,但有時(shí)設(shè)計(jì)為了App的整體美觀或者App產(chǎn)品本身的定位,絕大部分甚至所有字體,都需要使用自定義的字體。
如果再使用上面的方法逐一修改,對(duì)開發(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對(duì)象
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ù)具體需求,選擇對(duì)應(yīng)的方法