2013-07-25 49 views
4

好吧,我知道如何更改我的應用程序中單個文本框上使用的字體,但我希望我的應用程序中的所有文本使用此自定義字體和顏色,目前唯一的方法可以想到做他的是參考每個盒子並將它們設置爲適當的字體和顏色。雖然它是可行的,但它似乎是一種笨重的方法,有沒有更好的方法?更改默認字體爲Android應用程序

+0

,我認爲你必須編寫自己的風格和/或主題。看到這個有用的答案:http://stackoverflow.com/questions/8787690/adding-custom-font-to-theme-in-android或http://stackoverflow.com/questions/4395309/android-want-to-set -custom-fonts-for-whole-application-not-runtime或者這個http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android。 – JJ86

回答

8

您可以創建自定義的TextView並將它改寫到任何地方。

public class TypefacedTextView extends TextView { 

    public TypefacedTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); 
     setTypeface(typeface); 
    } 
} 

內view.xml用

<packagename.TypefacedTextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello"/> 
+1

我剛剛提交相同的答案:) – gunar

2

把你不同的字體文件夾資產...

TextView mytextView=(TextView)findViewById(R.id.txtbx); 
Typeface fonttype=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf"); 
mytextView.setTypeface(fonttype);