需要自己下载字体文件
一、自定义类继承
二、修改values/styles.xml文件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(); } } }
三、修改AndroidManifest.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等等<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);