2017-03-01 10 views
-1

カスタムフォントを実装するにはここではいくつかの例が異なりますが、私はapplication.i上で使用されている1つの抽象クラスでカスタムフォントを使用しています。ありがとうございます。 androidのtextviewでカスタムフォントを使用する方法

+0

https://futurestud.io/tutorials/custom-fonts-on-android-extending-textviewを確認してください – nnn

答えて

2

をこれを確認する詳細については、資産

// 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); 

フロンカスタムフォントのためにこれを試してみてください。 XMLで

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:

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

あなたは資産フォルダ

ArialMTBoldRegularTextView.javaにご.TFFファイルを追加する必要があります以下のようなカスタムクラスを作成します。

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> 

に次のように追加します。あなたのXMLから直接フォントを使用して

<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個の支持体は、詳細についてはdocを参照してください。

関連する問題