android 使用第三方字体(全局)

使用的是反射机制,通过搜索MONOSPACE的字体类型,把其字体设置成我们想要的字体
在自定义的Application里面声明全局字体。在assets中建立fonts文件夹,里面存放自定义的字体。
在style.xml声明全局的字体类型

public final class App extends MApplication{

    public static Typeface TypeFaceYaHei;
    @Override
    public void onCreate() {
        super.onCreate();
        TypeFaceYaHei = Typeface.createFromAsset(getAssets(), "fonts/PingHeiText.ttf");
        try
        {
            Field field = Typeface.class.getDeclaredField("MONOSPACE");
            field.setAccessible(true);
            field.set(null, TypeFaceYaHei);
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}

全局的数据类型

<style name="theme_fullScreen" parent="AppBaseTheme">

        <!-- 设置无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!--<span style="white-space:pre"></span>-->
        <item name="android:typeface">monospace</item>

    </style>

设置Application的主题

<application
        android:name="com.code.space.app.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/theme_fullScreen" >

单个控件引用外来字体

//设置了外来的字体
//        Typeface face = Typeface.createFromAsset (getAssets() , "fonts/SourceHanSansCN-ExtraLight.TTF" );
//        fabu_des.setTypeface (face);

猜你喜欢

转载自blog.csdn.net/pengpeng952789/article/details/79580755