Android 中全局更换字体的方法

在我们开发Android程序的时候通常会遇到更改全局字体的需求,我目前能想到的解决方案有三种,下面我们来进行逐一分析:

第一种方式就是自定义控件,毫无疑问这个一定能解决我们的问题,只需要把我们之前用的控件换成我们自定义的控件就好,缺点是如果是多种控件我们就要自定义多种相对应的View,太过麻烦,工作量太大

第二种是利用递归的思想遍历RootView 中的所有View进行判断并进行字体的更改,缺点是有损性能

第三种方法是利用setFactory方法来更换字体,这个也是我想主要说的,这里我们需要字体.ttf文件,如图:

Android 中全局更换字体的方法

主要代码如下:

[java] view plain copy
  1. package com.example.mac.allchangetextsizetest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.res.Configuration;  
  7. import android.graphics.Typeface;  
  8. import android.support.v4.view.LayoutInflaterCompat;  
  9. import android.support.v4.view.LayoutInflaterFactory;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.os.Bundle;  
  12. import android.support.v7.app.AppCompatDelegate;  
  13. import android.util.AttributeSet;  
  14. import android.util.DisplayMetrics;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.widget.Button;  
  18. import android.widget.EditText;  
  19. import android.widget.TextView;  
  20.   
  21. public class MainActivity extends AppCompatActivity {  
  22.     public static Typeface typeface;  
  23.     public static Typeface typeface1;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         if (typeface == null)  {  
  28.             typeface = Typeface.createFromAsset(getAssets(), "Arial.ttf");  
  29.         }  
  30.         if (typeface1 == null {  
  31.             typeface1 = Typeface.createFromAsset(getAssets(), "ARIALNI.TTF");  
  32.         }  
  33.         LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory()  
  34.         {  
  35.             @Override  
  36.             public View onCreateView(View parent, String name, Context context, AttributeSet attrs)  
  37.             {  
  38.                 AppCompatDelegate delegate = getDelegate();  
  39.                 View view = delegate.createView(parent, name, context, attrs);  
  40.   
  41.                 if ( view!= null && (view instanceof TextView))   {  
  42.                     ((TextView) view).setTypeface(typeface1);  
  43.                 }  
  44.                 if(view!=null && (view instanceof EditText)) {  
  45.                     ((EditText) view).setTypeface(typeface);  
  46.                 }  
  47.                 return view;  
  48.             }  
  49.         });  
  50.         super.onCreate(savedInstanceState);  
  51.         setContentView(R.layout.activity_main);  
  52.   
  53.         Button button = (Button)findViewById(R.id.button);  
  54.         button.setOnClickListener(new View.OnClickListener() {  
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                // changeTextSize(MainActivity.this,2);  
  58.                 finish();  
  59.                 Intent intent = new Intent(MainActivity.this, MainActivity.class);  
  60.                 startActivity(intent);  
  61.             }  
  62.         });  
  63.   
  64.     }  
这样只要我们的Activity 继承自上面的Activity那么其中的TextView 和 EditText 都会变成我们设定成的字体

效果图如下:

Android 中全局更换字体的方法

这样 我们的变换字体就OK了