2017-03-01 43 views
-1

對於實現自定義字體,我在這裏看到幾個例子問題是不同的,我在一個抽象類中使用自定義字體,這是在所有的應用程序中使用。我無法更改字體。 在此先感謝。如何在textview中使用自定義字體android

+0

檢查https://futurestud.io/tutorials/custom-fonts-on-android-extending-text-view – nnn

回答

1

嘗試一下本作自定義字體弗朗資產

// Font path 
     String fontPath = "fonts/Face Your Fears.ttf"; 

     // text view label 
     TextView txtGhost = (TextView) findViewById(R.id.ghost); 

     // Loading Font Face 
     Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

     // Applying font 
     txtGhost.setTypeface(tf); 

的更多信息,請此, http://www.androidhive.info/2012/02/android-using-external-fonts/

2

你可以這樣做。 你要添加的文件夾資產

ArialMTBoldRegularTextView.java您.TFF文件:

public final class ArialMTBoldRegularTextView extends CustomTextView { 

    public static final String FONT_PATH = "arial-rounded-mt-bold.ttf"; 

    public ArialMTBoldRegularTextView(Context context) { 
     super(context); 
     setFont(FONT_PATH); 
    } 

    public ArialMTBoldRegularTextView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
     setFont(FONT_PATH); 
    } 

    public ArialMTBoldRegularTextView(Context context, AttributeSet attributeSet, int defStyleAttr) { 
     super(context, attributeSet, defStyleAttr); 
     setFont(FONT_PATH); 
    } 

    public void setFont(String fontPath) { 
     changeFont(this, fontPath); 
    } 



public static void changeFont(final CompoundButton button, final String fontPath) { 
    Typeface typeface = Typeface.createFromAsset(button.getContext().getAssets(), fontPath); 
    button.setTypeface(typeface); 
} 
} 

CustomTextView.java

public class CustomTextView extends TextView { 

    public CustomTextView(Context context) { 
     super(context); 
    } 

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

    public CustomTextView(Context context, AttributeSet attributeSet, int defStyleAttr) { 
     super(context, attributeSet, defStyleAttr); 
    } 

    public Typeface getFont(final Context context, final String fontPath) { 
     return Typeface.createFromAsset(context.getAssets(), fontPath); 
    } 

    public void changeFont(final TextView textView, final String fontPath) { 
     Typeface typeface = Typeface.createFromAsset(textView.getContext().getAssets(), fontPath); 
     textView.setTypeface(typeface); 
    } 

    public void changeFont(final CompoundButton button, final String fontPath) { 
     Typeface typeface = Typeface.createFromAsset(button.getContext().getAssets(), fontPath); 
     button.setTypeface(typeface); 
    } 
} 

和fater以xml:

<packagename.views.ArialMTBoldRegularTextView 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:gravity="center" 
      android:text="Pseudo"/> 
4

像下面創建自定義類。

public class CustomTextView extends TextView { 

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

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

    } 

    public CustomTextView(Context context) { 
     super(context); 
     init(null); 
    } 

    private void init(AttributeSet attrs) { 
     if (attrs!=null) { 
      TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView); 
      String fontName = a.getString(R.styleable.CustomTextView_fontName); 
      if (fontName!=null) { 
       Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName); 
       setTypeface(myTypeface); 
      } 
      a.recycle(); 
     } 
    } 

} 

並將您的字體添加到assets> fonts文件夾中。

加入attrs.xml

<declare-styleable name="CustomTextView"> 
     <attr name="fontName" format="string" /> 
    </declare-styleable> 

使用以下。

<com.abc.cusomclass.CustomTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:fontName="/*name of your font from assets/font folder*/"/> 
1

現在支持Android庫26個支持直接使用的字體從您的XML看到doc瞭解更多詳情。

相關問題