由于最近的项目需要改变字体,所以用到了现在的方法,下面的总结是从网上整理出来的。但是还是比较好用的。希望能帮助大家省些时间。
public
class
Y_fonts
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* 必须事先在assets底下创建一fonts文件夹 并放入要使用的字体文件(.ttf)
* 并提供相对路径给creatFromAsset()来创建Typeface对象
*/
Typeface fontFace = Typeface.createFromAsset(getAssets(),
"fonts/STXINGKA.TTF");
// 字体文件必须是true type font的格式(ttf);
// 当使用外部字体却又发现字体没有变化的时候(以 Droid Sans代替),通常是因为
// 这个字体android没有支持,而非你的程序发生了错误
TextView text = (TextView) findViewById(R.id.ttf);
text.setTypeface(fontFace);
}
}
// 得到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