所謂無圖無真相,先看效果圖:
Java代碼如下:
package yc.android.fonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
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);
}
}
PS:
我使用的字體是華文行楷
由於Android系統對字體文件的支持問題,該字體文件2.3.3版本以上有效,2.2版本不支持。