您的位置:首页 > 移动开发 > Android开发

Android 改变全局字体

2015-08-05 17:52 155 查看
使用的是反射机制,通过搜索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" >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: