Android 使用反射更换app全部字体

需要自己下载字体文件

一、自定义类继承

public class SetAppTypeface extends MultiDexApplication {
    public static Typeface typeFace;

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(false);
        setTypeface();
    }
    public void setTypeface(){
        typeFace = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Regular.ttf");
        try
        {
            //与values/styles.xml中的<item name="android:typeface">serif</item>对应
//            Field field = Typeface.class.getDeclaredField("SERIF");
//            field.setAccessible(true);
//            field.set(null, typeFace);

//            Field field_1 = Typeface.class.getDeclaredField("DEFAULT");
//            field_1.setAccessible(true);
//            field_1.set(null, typeFace);

            //与monospace对应
//            Field field_2 = Typeface.class.getDeclaredField("MONOSPACE");
//            field_2.setAccessible(true);
//            field_2.set(null, typeFace);

            //与values/styles.xml中的<item name="android:typeface">sans</item>对应
            Field field_3 = Typeface.class.getDeclaredField("SANS_SERIF");
            field_3.setAccessible(true);
            field_3.set(null, typeFace);
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}
二、修改values/styles.xml文件
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="SetAppFont" parent="AppTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:typeface">sans</item>
    </style>

    
    
这里android:typeface可以设置的仅仅有normal、sans、serif、monospace可以设置,因为我在SetAppTypeface类中设置的是Typeface.class.getDeclaredField("SANS_SERIF"); 所以我这里便设置成sans,如果getDeclaredField()设置的是其他的类型,则要选择同类型的其他诸如serif、monospace等等
三、修改AndroidManifest.xml

<application
        android:name=".SetAppTypeface"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/SetAppFont">
        <activity
            android:name="com.yjcul.activity.MainActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

这样即可修改app所有的字体。
如果有些字体想换回去可以单独设置
    textView01=(TextView)super.findViewById(R.id.TextView01);
    textView01.setTypeface(null, Typeface.NORMAL);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值