首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android 自定义字体(引来外部字体)

2012-09-03 
Android 自定义字体(引入外部字体)做Android开发的时候,一些软件会要求一些特殊字体,我们需要引入外部的tt

Android 自定义字体(引入外部字体)

做Android开发的时候,一些软件会要求一些特殊字体,我们需要引入外部的ttf格式的字体到程序中,具体操作步骤为:

在安卓应用程序的目录assets中新建fonts目录,将我们需要使用的ttf字体文件复制进去,然后代码:

// 得到TextView控件对象TextView textView = (TextView) findViewById(R.id.custom);// 将字体文件保存在assets/fonts/目录下,www.linuxidc.com创建Typeface对象Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");// 应用字体textView.setTypeface(typeFace);
如果想整个页面都使用同样的字体,可以使用批处理,新增一个Java类,如下:

public class FontManager {          public static void changeFonts(ViewGroup root, Activity act) {             Typeface tf = Typeface.createFromAsset(act.getAssets(),                "fonts/xxx.ttf");             for (int i = 0; i < root.getChildCount(); i++) {             View v = root.getChildAt(i);             if (v instanceof TextView) {                ((TextView) v).setTypeface(tf);             else if (v instanceof Button) {                ((Button) v).setTypeface(tf);             else if (v instanceof EditText) {                ((EditText) v).setTypeface(tf);             else if (v instanceof ViewGroup) {                changeFonts((ViewGroup) v, act);             }         }          }  




热点排行