简体   繁体   English

如何在Android中的TextView上使用自定义字体

[英]how to use custom fonts on textview in android

For implementing custom font i seen few examples here issue is different,I am taking custom font in one abstract class which is used in all over the application.i am unable to change the fonts. 为了实现自定义字体,我在这里看到的几个示例的问题是不同的,我将自定义字体放在整个应用程序中使用的一个抽象类中。我无法更改字体。 thanks in advance. 提前致谢。

Create the custom class like below. 创建如下的自定义类。

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

}

and add your font in assets>fonts folder. 并将字体添加到Assets> fonts文件夹中。

add following in attrs.xml 在attrs.xml中添加以下内容

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

to use. 使用。

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

You can do like this. 你可以这样 You have to add your .tff file on the assets folder 您必须在资产文件夹中添加.tff文件

ArialMTBoldRegularTextView.java: ArialMTBoldRegularTextView.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);
}
}

CustomTextView.java 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);
    }
}

and fater in xml: 在xml中更胖:

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

try this for custom font 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);

for more information check this, http://www.androidhive.info/2012/02/android-using-external-fonts/ 有关更多信息,请检查此内容, http://www.androidhive.info/2012/02/android-using-external-fonts/

现在android支持库26支持直接从XML使用字体。有关更多详细信息,请参阅文档

The better way of doing this is to make custom TextView with custom font like this: 更好的方法是使用以下自定义字体制作自定义TextView:

Java 爪哇

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格式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM