android 字体的切换,Android为整个应用切换第三方字体

该博客详细介绍了如何通过反射技术在Android应用中替换系统默认字体,包括在Application类中设置全局字体、遍历并修改ViewGroup下的文本控件字体以及创建自定义TextView控件来设置字体。此外,还提供了在XML布局中使用自定义字体的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

1、通过反射来获取系统字体并替换

import java.lang.reflect.Field;

import android.content.Context;

import android.graphics.Typeface;

public final class FontsOverride {

public static void setDefaultFont(Context context,

String staticTypefaceFieldName, String fontAssetName) {

final Typeface regular = Typeface.createFromAsset(context.getAssets(),

fontAssetName);

replaceFont(staticTypefaceFieldName, regular);

}

protected static void replaceFont(String staticTypefaceFieldName,

final Typeface newTypeface) {

try {

final Field staticField = Typeface.class

.getDeclaredField(staticTypefaceFieldName);

staticField.setAccessible(true);

staticField.set(null, newTypeface);

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

}

2、在Application类中执行修改字体(替换系统默认字体)

public final class Application extends android.app.Application {

@Override

public void onCreate() {

super.onCreate();

FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");

FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");

FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");

FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");

}

}

二、

配置文件AndroidManifest.xml中修改(只限系统theme中已有字体)

monospace

三、遍历viewGroup下所有文本控件并设置字体

/**

* Recursively sets a [email protected] Typeface} to all

* [email protected] TextView}s in a [email protected] ViewGroup}.

*/

public static final void setAppFont(ViewGroup mContainer, Typeface mFont, boolean reflect)

{

if (mContainer == null || mFont == null) return;

final int mCount = mContainer.getChildCount();

// Loop through all of the children.

for (int i = 0; i < mCount; ++i)

{

final View mChild = mContainer.getChildAt(i);

if (mChild instanceof TextView)

{

// Set the font if it is a TextView.

((TextView) mChild).setTypeface(mFont);

}

else if (mChild instanceof ViewGroup)

{

// Recursively attempt another ViewGroup.

setAppFont((ViewGroup) mChild, mFont);

}

else if (reflect)

{

try {

Method mSetTypeface = mChild.getClass().getMethod("setTypeface", Typeface.class);

mSetTypeface.invoke(mChild, mFont);

} catch (Exception e) { /* Do something... */ }

}

}

}

final Typeface mFont = Typeface.createFromAsset(getAssets(),

"fonts/MyFont.ttf");

final ViewGroup mContainer = (ViewGroup) findViewById(

android.R.id.content).getRootView();

HomeActivity.setAppFont(mContainer, mFont);

四、自定义控件并设置字体

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

}

}

android:id="@+id/tvtitle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hi Android"

android:textAppearance="?android:attr/textAppearanceLarge"

/>

原文:http://blog.youkuaiyun.com/android_farmer/article/details/43638583

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值