2012-04-10 82 views
1

我想爲我的項目分配一個不同的字體。 我希望新的字體是適用於整個項目,但我覺得是對字體更改爲一個TextViewandroid項目中的自定義字體

Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf"); 
TextView customText1 = (TextView)findViewById(R.id.text1); 
customText1.setTypeface(font1); 
customText1.setTextSize(40.f); 
customText1.setText("Hello! This is a custom font..."); 

有任何方式默認爲整個項目自定義字體? 最好的方面

回答

0

不完全是你要求的,但建立在上面的評論有辦法讓使用自定義字體與默認控件更容易。

這顯示瞭如何擴展TextView並使用自定義屬性,以便TextView支持自定義字體。
Custom fonts and XML layouts (Android)

0

我所做的是創建一個支持類並從我的活動中實例化它並傳遞我希望樣式化的所有視圖。

public class textfactory{ 
private TextView tv; 
private Button b; 
private RadioButton rb; 

private TypeFace font; 

/** 
* fetch font resource 
*/ 
public textfactory(Context context){ 
    this.font = Typeface.createFromAsset(context.getAssets(), 'customfont.ttf'); 
} 
/** 
* pass in all the views you wish to apply font to 
*/ 
public void style(View... views){ 
    for(View v : views){ 
     if(v instance of TextView) 
     { 
      tv = (TextView)v; 
      tv.setTypeface(this.font); 
     } 
     else if(v instance of Button) 
     { 
      b = (Button)v; 
      b.setTypeface(this.font); 
     } 
     else if(v instance of RadioButton) 
     { 
      rb = (RadioButton)v; 
      rb.setTypeface(this.font); 
     } 
     //add as many view conditionals as required 
    } 
} 
}