2013-10-21 72 views
1

我已經改變使用如何將字體更改爲Xml中定義的默認值?

Typeface font = Typeface.createFromAsset(this.getAssets(),"fonts/Arial.ttf"); 

我Textviews的字體在我的XML佈局我有不同的字體許多textviews,我想保留這個任何時候編程

試過以下

Typeface.DEFAULT 

Typeface font =null;

所有的文字瀏覽都設置爲相同的字體而不是Xml中的文字

如何保留字體而不必重新啓動我的應用程序?

回答

0

步驟1)創建一個類名CustomTextView並複製粘貼此代碼,但確保自定義字體應該是Project Assets文件夾。所以你可以替換字體名稱。

包YOUR_PACKAGE;

public class CustomTextView extends TextView{ 

    public CustomButton(Context context) { 
     super(context); 
     setFont(); 

    } 

    public CustomTextView (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setFont(); 
    } 

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setFont(); 
    } 

    private void setFont() { 
     Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf"); 
     setTypeface(normal, Typeface.NORMAL); 

    } 
} 

步驟2)

使用該自定義你的佈局的TextView

<LinearLayout 
     android:id="@+id/signing_details" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
     android:weightSum="100" > 

<YOUR_PACKAGE.CustomTextView 
       android:id="@+id/textview" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/app_name" 
       android:textSize="16sp" /> 
</ LinearLayout> 

您也可以使用此CustomTextView動態

CustomTextView textView = new CustomTextView (this); 
textView.setText(res.getString(R.string.app_name)); 
textView.setTextColor(Color.parseColor("#000000")); 
textView.setTextSize(16); 

希望這有助於你。

相關問題