android 开发 app全局字体的设置

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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值