Android介绍两种全局修改字体的方式

修改安卓字体

第一种方式:在BaseActivity/BaseFragment 内

LayoutInflaterCompat

public class ModifyFontActivity extends AppCompatActivity {
private Typeface mTypeface;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory2(LayoutInflater.from(this), new LayoutInflater.Factory2()
{
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return  replaceFont(null,name, context, attrs);
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return  replaceFont(parent,name, context, attrs);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modifyfont);
}
//替换字体
private View replaceFont(View Parent,String name, Context context, AttributeSet attrs) {
if (mTypeface==null)Typeface.createFromAsset(getAssets(),"fonts/PingFangSC-Semibold.ttf");
Log.e("replaceFont: ","name "+name );
if ("TextView".equals(name)) {
TextView textView = new TextView(context, attrs);
textView.setTextColor(Color.parseColor("#4B4B52"));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
textView.setTypeface(mTypeface);
return textView;
}else if ("android.support.v7.widget.AppCompatTextView".equals(name)){
// ......
}
return null;
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,在根布局内,我是默认字体"
android:layout_marginTop="36dp"
android:layout_gravity="center_horizontal"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,在里一层RelativeLayout内"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,在里二层LinearLayout内"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,在里三层LinearLaout布局内"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,在里四层LinearLayout内"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

运行效果:

下面主要介绍第二种方式:

1. 在Application下的主题添加默认的TypeFace属性

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

<item name="android:typeface">monospace</item>

</style>

2. 自定义MyApp继承Application(需要在AndroidManifest.xml中申明)

3. 在MyApp的oncreate()通过反射方法中修改APP默认字体:

具体代码如下

public void replaceSystemDefaultFont(Context context, String fontPath) {

replaceTypefaceField("MONOSPACE", Typeface.createFromAsset(context.getAssets(), fontPath));

}

//关键--》通过修改MONOSPACE字体为自定义的字体达到修改app默认字体的目的

private void replaceTypefaceField(String fieldName, Object value) {

try {

Field defaultField = Typeface.class.getDeclaredField(fieldName);

defaultField.setAccessible(true);

defaultField.set(null, value);

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

//字体地址,一般放置在assets/fonts目录

String fontPath = "fonts/test.ttf";

@Override

public void onCreate() {

super.onCreate();

replaceSystemDefaultFont(getApplicationContext(), fontPath);

}

网上的工具类:

在继承Application类的oncreate()

FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this,"fonts/NotoSansCJKsc-Thin.otf");

package com.test.fontfamily;
import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/10/24.
*/
public class FontUtils
{
private static final String TAG = FontUtils.class.getSimpleName();
private Map<String, SoftReference<Typeface>> mCache = new HashMap<>();
private static FontUtils sSingleton = null;
public static Typeface DEFAULT = Typeface.DEFAULT;
// disable instantiate
private FontUtils()
{
}
public static FontUtils getInstance()
{
// double check
if (sSingleton == null)
{
synchronized (FontUtils.class)
{
if (sSingleton == null)
{
sSingleton = new FontUtils();
}
}
}
return sSingleton;
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root     The root view.
* @param fontPath font file path relative to 'assets' directory.
*/
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath)
{
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath));
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root     The root view.
* @param fontPath font file path relative to 'assets' directory.
* @param style    One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style)
{
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style);
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root     The root view.
* @param fontPath The full path to the font data.
*/
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath)
{
replaceFont(root, createTypefaceFromFile(fontPath));
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root     The root view.
* @param fontPath The full path to the font data.
* @param style    One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style)
{
replaceFont(root, createTypefaceFromFile(fontPath), style);
}
/**
* <p>Replace the font of specified view and it's children with specified typeface</p>
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface)
{
if (root == null || typeface == null)
{
return;
}
if (root instanceof TextView)
{ // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
// Extract previous style of TextView
int style = Typeface.NORMAL;
if (textView.getTypeface() != null)
{
style = textView.getTypeface().getStyle();
}
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup)
{ // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i)
{
replaceFont(viewGroup.getChildAt(i), typeface);
}
} // else return
}
/**
* <p>Replace the font of specified view and it's children with specified typeface and text style</p>
*
* @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style)
{
if (root == null || typeface == null)
{
return;
}
if (style < 0 || style > 3)
{
style = Typeface.NORMAL;
}
if (root instanceof TextView)
{ // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup)
{ // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i)
{
replaceFont(viewGroup.getChildAt(i), typeface, style);
}
} // else return
}
/**
* <p>Create a Typeface instance with specified font file</p>
*
* @param fontPath font file path relative to 'assets' directory.
* @return Return created typeface instance.
*/
private Typeface createTypefaceFromAsset(Context context, String fontPath)
{
SoftReference<Typeface> typefaceRef = mCache.get(fontPath);
Typeface typeface = null;
if (typefaceRef == null || (typeface = typefaceRef.get()) == null)
{
typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
typefaceRef = new SoftReference<>(typeface);
mCache.put(fontPath, typefaceRef);
}
return typeface;
}
private Typeface createTypefaceFromFile(String fontPath)
{
SoftReference<Typeface> typefaceRef = mCache.get(fontPath);
Typeface typeface = null;
if (typefaceRef == null || (typeface = typefaceRef.get()) == null)
{
typeface = Typeface.createFromFile(fontPath);
typefaceRef = new SoftReference<>(typeface);
mCache.put(fontPath, typefaceRef);
}
return typeface;
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*
* @param context  {@link Context Context}
* @param fontPath font file path relative to 'assets' directory.
*/
public void replaceSystemDefaultFontFromAsset(@NonNull Context context, @NonNull String fontPath)
{
replaceSystemDefaultFont(createTypefaceFromAsset(context, fontPath));
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*
* @param context  {@link Context Context}
* @param fontPath The full path to the font data.
*/
public void replaceSystemDefaultFontFromFile(@NonNull Context context, @NonNull String fontPath)
{
replaceSystemDefaultFont(createTypefaceFromFile(fontPath));
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*/
private void replaceSystemDefaultFont(@NonNull Typeface typeface)
{
modifyObjectField(null, "MONOSPACE", typeface);
}
private void modifyObjectField(Object obj, String fieldName, Object value)
{
try
{
Field defaultField = Typeface.class.getDeclaredField(fieldName);
defaultField.setAccessible(true);
defaultField.set(obj, value);
} catch (NoSuchFieldException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}
(0)

相关推荐