2012-03-02 35 views
4

我知道豪設置自定義字體編程Android應用程序內。 有沒有什麼方法來加載自定義字體(資產)的字體和Android框架將使用基於大膽,斜體等適當的文件?的Android設置的Roboto字體,加粗,斜體,普通,...(有點像自定義字體家族)

比如現在我想的Roboto字體設置爲某個TextView

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); 
textView.setTypeface(typeface); 

它工作正常。但因爲我設置TextView中的XML佈局大膽,文本不加粗

<TextView 
    android:id="@+id/my_id" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="50dp" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:text="@string/my_text" 
    android:textColor="@color/my_foreground" 
    android:textSize="24dp" /> 

如何從資產加載字體正確,這將工作?

textView.setTypeface(typeface, Typeface.BOLD); 

在我的資產目錄中只有一個「字體系列」

Roboto-Black.ttf 
Roboto-BlackItalic.ttf 
Roboto-Bold.ttf 
Roboto-BoldCondensed.ttf 
Roboto-BoldCondensedItalic.ttf 
Roboto-BoldItalic.ttf 
Roboto-Condensed.ttf 
Roboto-CondensedItalic.ttf 
Roboto-Italic.ttf 
Roboto-Light.ttf 
Roboto-LightItalic.ttf 
Roboto-Medium.ttf 
Roboto-MediumItalic.ttf 
Roboto-Regular.ttf 
Roboto-Thin.ttf 
Roboto-ThinItalic.ttf 

如何加載所有排字體/家庭裏面的字體?

回答

2

我不知道如何將單個字體的不同字體變體作爲一個家族來處理,但字體往往會有較大的文件大小,因此您可能不希望將所有這些字體導入到您的應用中。相反,您可以只使用Medium字體,然後爲其設置粗體和斜體屬性。

舉例來說,如果你有安卓TEXTSTYLE =「黑體」在你的XML佈局設置,你可以在你的代碼,以大膽的風格做到這一點:

Typeface currentTypeFace = textView.getTypeface(); 
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) { 
    textView.setTypeface(tf, Typeface.BOLD); 
} else { 
    textView.setTypeface(tf); 
}