Android 应用ttf字体

有时候为了app的美观可能会使用第三方字体,下面介绍几种app使用第三方字体的方法。

第一种,通过反射全局设置app字体,这个方法简单、粗暴、高效,推荐使用,下面介绍怎么使用。

1、首先继承application类并重写oncreate方法

2、通过反射方式设置资源字体

  1. public class App extends Application {  
  2.     public static Typeface typeFace;  
  3.   
  4.     @Override  
  5.     public void onCreate() {  
  6.         super.onCreate();  
  7.         //在app启动创建时调用  
  8.         setTypeface();  
  9.     }  
  10.   
  11.     /** 
  12.      * 通过反射方法设置app全局字体 
  13.      */  
  14.     public void setTypeface(){  
  15.         typeFace = Typeface.createFromAsset(getAssets(), ”fonts/aaa.ttf”);  
  16.         try  
  17.         {  
  18.             Field field = Typeface.class.getDeclaredField(“SERIF”);  
  19.             field.setAccessible(true);  
  20.             field.set(null, typeFace);  
  21.         }  
  22.         catch (NoSuchFieldException e)  
  23.         {  
  24.             e.printStackTrace();  
  25.         }  
  26.         catch (IllegalAccessException e)  
  27.         {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31. }  
public class App extends Application {
    public static Typeface typeFace;

    @Override
    public void onCreate() {
        super.onCreate();
        //在app启动创建时调用
        setTypeface();
    }

    /**
     * 通过反射方法设置app全局字体
     */
    public void setTypeface(){
        typeFace = Typeface.createFromAsset(getAssets(), "fonts/aaa.ttf");
        try
        {
            Field field = Typeface.class.getDeclaredField("SERIF");
            field.setAccessible(true);
            field.set(null, typeFace);
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}

3、在manifest文件中配置application和主题

4、主题中加入<itemname=“android:typeface”>serif</item>



需要注意的的对于父主题的选择上,不要使用android:Theme.DeviceDefault开始的主题,因为这样就反射设置的字体就无法生效。

第二种,单个设置textview,这样比较的麻烦,textview有一个setTypeFace()方法,这样就能改变字体样式,这个方法不推荐使用。

第三种,比如说要所有的textview都要用第三方字体,那么就重写TextView,上面也说了textview有setTypeFace方法,将某人的字体替换成我们想要的就可以了。

  1. public class CusFntTextView extends TextView {  
  2.    
  3. public CusFntTextView(Context context, AttributeSet attrs, int defStyle) {  
  4.     super(context, attrs, defStyle);  
  5.     init();  
  6. }  
  7.    
  8. public CusFntTextView(Context context, AttributeSet attrs) {  
  9.     super(context, attrs);  
  10.     init();  
  11. }  
  12.    
  13. public CusFntTextView(Context context) {  
  14.     super(context);  
  15.     init();  
  16. }  
  17.    
  18. private void init() {  
  19.     if (!isInEditMode()) {  
  20.         Typeface tf = Typeface.createFromAsset(getContext().getAssets(), ”Futura.ttf”);  
  21.         setTypeface(tf);  
  22.     }  
  23. }  
public class CusFntTextView extends TextView {

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

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

public CusFntTextView(Context context) {
    super(context);
    init();
}

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Futura.ttf");
        setTypeface(tf);
    }
}
第四种,github上有个开源的库简单的设置下就能使用字体,贴上介绍的帖子 点击访问

第五种,遍历根节点,依次为字控件设置字体,不推荐这样使用,效率不高,浪费资源体验也不好

最后贴上效果图和demo的下载地址   点击下载



有时候为了app的美观可能会使用第三方字体,下面介绍几种app使用第三方字体的方法。

第一种,通过反射全局设置app字体,这个方法简单、粗暴、高效,推荐使用,下面介绍怎么使用。

1、首先继承application类并重写oncreate方法

2、通过反射方式设置资源字体

  1. public class App extends Application {  
  2.     public static Typeface typeFace;  
  3.   
  4.     @Override  
  5.     public void onCreate() {  
  6.         super.onCreate();  
  7.         //在app启动创建时调用  
  8.         setTypeface();  
  9.     }  
  10.   
  11.     /** 
  12.      * 通过反射方法设置app全局字体 
  13.      */  
  14.     public void setTypeface(){  
  15.         typeFace = Typeface.createFromAsset(getAssets(), ”fonts/aaa.ttf”);  
  16.         try  
  17.         {  
  18.             Field field = Typeface.class.getDeclaredField(“SERIF”);  
  19.             field.setAccessible(true);  
  20.             field.set(null, typeFace);  
  21.         }  
  22.         catch (NoSuchFieldException e)  
  23.         {  
  24.             e.printStackTrace();  
  25.         }  
  26.         catch (IllegalAccessException e)  
  27.         {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31. }  
public class App extends Application {
    public static Typeface typeFace;

    @Override
    public void onCreate() {
        super.onCreate();
        //在app启动创建时调用
        setTypeface();
    }

    /**
     * 通过反射方法设置app全局字体
     */
    public void setTypeface(){
        typeFace = Typeface.createFromAsset(getAssets(), "fonts/aaa.ttf");
        try
        {
            Field field = Typeface.class.getDeclaredField("SERIF");
            field.setAccessible(true);
            field.set(null, typeFace);
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}

3、在manifest文件中配置application和主题

4、主题中加入<itemname=“android:typeface”>serif</item>



需要注意的的对于父主题的选择上,不要使用android:Theme.DeviceDefault开始的主题,因为这样就反射设置的字体就无法生效。

第二种,单个设置textview,这样比较的麻烦,textview有一个setTypeFace()方法,这样就能改变字体样式,这个方法不推荐使用。

第三种,比如说要所有的textview都要用第三方字体,那么就重写TextView,上面也说了textview有setTypeFace方法,将某人的字体替换成我们想要的就可以了。

  1. public class CusFntTextView extends TextView {  
  2.    
  3. public CusFntTextView(Context context, AttributeSet attrs, int defStyle) {  
  4.     super(context, attrs, defStyle);  
  5.     init();  
  6. }  
  7.    
  8. public CusFntTextView(Context context, AttributeSet attrs) {  
  9.     super(context, attrs);  
  10.     init();  
  11. }  
  12.    
  13. public CusFntTextView(Context context) {  
  14.     super(context);  
  15.     init();  
  16. }  
  17.    
  18. private void init() {  
  19.     if (!isInEditMode()) {  
  20.         Typeface tf = Typeface.createFromAsset(getContext().getAssets(), ”Futura.ttf”);  
  21.         setTypeface(tf);  
  22.     }  
  23. }  
public class CusFntTextView extends TextView {

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

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

public CusFntTextView(Context context) {
    super(context);
    init();
}

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Futura.ttf");
        setTypeface(tf);
    }
}
第四种,github上有个开源的库简单的设置下就能使用字体,贴上介绍的帖子 点击访问

第五种,遍历根节点,依次为字控件设置字体,不推荐这样使用,效率不高,浪费资源体验也不好

最后贴上效果图和demo的下载地址   点击下载



猜你喜欢

转载自blog.csdn.net/qq_21937107/article/details/80220884
今日推荐