2012-02-17 9 views
0

私はAndroidのカスタムフォントを使いこなしています。私はフォントをインポートする方法を知って、そのフォントに設定されたテキストビューを持っています。私はあなたが多くのtextViewsを持っていれば、これはむしろ面倒になることに気づいた。レイアウト全体のフォントをandroidで設定する

レイアウトフォントタイプ全体を特定のフォントフェイスに設定する方法はありますか?

ありがとうございます。

+1

この質問を参照してください:http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android – howettl

答えて

1

私はこのようなコンストラクタで書体を設定するのTextViewの私自身のサブクラスを宣言してやった:私は完全修飾名を使用する場合

public class MyTextView extends TextView { 

public MyTextView(Context context) { 
    super(context); 
    setTypeFace(); 
} 


public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setTypeFace(); 
} 


public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setTypeFace(); 
} 

public void setTypeFace() 
{ 
    this.setTypeface(StaticUtils.getDefaultFontNormal(getContext())); 
} 

} 

その後、私のレイアウトで、それは動作します:

<ca.mycompany.mobile.ui.support.MyTextView 
     android:id="@+id/title_summaryreports" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dip" 
      android:paddingTop="10dip" 
      android:text="@string/title_strategies" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textStyle="bold" 
      android:textColor="#ff0000" /> 
0

あなたはただ、この方法には、トップレベルのviewgroups IDおよびフォントの書体を渡し、このメソッドを使用することができ、これはあなたの全体のレイアウトにフォントを設定します。

public void setFont(ViewGroup group, Typeface lTypeface) 
{ 
    int count = group.getChildCount(); 
    View v; 
    for (int i = 0; i < count; i++) 
    { 
     v = group.getChildAt(i); 
     if (v instanceof TextView) 
     { 
      ((TextView) v).setTypeface(lTypeface); 
     } else if (v instanceof EditText) 
     { 
      ((EditText) v).setTypeface(lTypeface); 
     } else if (v instanceof Button) 
     { 
      ((Button) v).setTypeface(lTypeface); 
     } else if (v instanceof TextInputLayout) 
     { 
      ((TextInputLayout) v).setTypeface(lTypeface); 
     } else if (v instanceof ViewGroup) 
      setFont((ViewGroup) v, lTypeface); 
    } 
} 
関連する問題