TextView使用指定的TTF字庫

 public class FontTest 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 font = Typeface.createFromAsset(getAssets(), "fonts/sans.ttf");    

           // 字體文件必須是true type font的格式(ttf);   

          // 當使用外部字體卻又發現字體沒有變化的時候(以 Droid Sans代替),通常是因爲這個字體android沒有支持,而非你的程序發生了錯誤            

           TextView text = (TextView)findViewById(R.id.text);     

            text.setTypeface(font);     

            text.setText("Hello Android!");     

            text.setTextSize(40);     

            }     

}   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章