如何在android中的textview上使用自定义字体

时间:2017-03-01 10:21:09

标签: android

为了实现自定义字体我看到这里的几个例子问题是不同的,我在一个抽象类中使用自定义字体,它在整个应用程序中使用。我无法更改字体。 提前谢谢。

5 个答案:

答案 0 :(得分: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();
        }
    }

}

并在资源> 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 :(得分:2)

你可以这样做。 您必须在资产文件夹

上添加.tff文件

ArialMTBoldRegularTextView.java:

SPLIT_SLOP

CustomTextView.java

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

并且更适合xml:

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

答案 2 :(得分:1)

尝试使用自定义字体fron Assets

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

答案 3 :(得分:1)

现在,android Support Library 26支持直接使用XML中的字体,请参阅doc了解更多详情。

答案 4 :(得分:0)

更好的方法是使用自定义字体制作自定义TextView,如下所示:

<强>爪哇

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;


public class RegularProximaTextView extends TextView {
   public static Typeface FONT_NAME;


   public RegularProximaTextView(Context context) {
    super(context);
    if(FONT_NAME == null) FONT_NAME = 
  Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - 
  Proxima Nova Regular.otf");
    this.setTypeface(FONT_NAME);
  }
  public RegularProximaTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
     if(FONT_NAME == null) FONT_NAME = 
    Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - 
      Proxima 
      Nova Regular.otf");
      this.setTypeface(FONT_NAME);
   }
  public RegularProximaTextView(Context context, AttributeSet attrs, int 
  defStyle) {
    super(context, attrs, defStyle);
    if(FONT_NAME == null) FONT_NAME = 
       Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - 
       Proxima Nova Regular.otf");
       this.setTypeface(FONT_NAME);
    }
 }

<强> XML

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.design.widget.CoordinatorLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_login"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true"
  android:background="@color/colorGreyBar"
 >


   <com.tracer.joblogic.v2.helpers.custom_ui.RegularProximaTextView
            android:id="@+id/tvTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:textColor="@color/colorButtonRed"
            android:text="Some text"
            android:textSize="8sp"
            />
 </android.support.design.widget.CoordinatorLayout>