android 开发时系统有默认的字体,但是若在app中使用其他字体,就需要用到字体的.ttf资源文件
切入正题:首先创建assets文件夹:
在assets文件夹下创建fonts目录,用来存放aaa.ttf字体资源文件(字体文件是.ttf格式的)
下面要上代码喽!
MyApplication中设置读取的资源文件:
package com.cbule.my.testdemo;
import android.app.Application;
import android.graphics.Typeface;
import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2016/10/17.
*/
public class MyApplication extends Application {
public static Typeface typeFace;
@Override
public void onCreate() {
super.onCreate();
setTypeface();
}
public void setTypeface(){
typeFace = Typeface.createFromAsset(getAssets(), "fonts/aaa.ttf");
try
{
Field field = Typeface.class.getDeclaredField("SERIF");
field.setAccessible(true);
field.set(null, typeFace);
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}
在Styles文件中appTheme下设置: serif
<!-- Base application theme. -->
<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>
<item name="android:typeface">serif</item>
</style>
在这里我用的是MyApplication继承application,所以最后不要忘了在androidMinifests文件中,对application写入name 标签哦
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>